1

WPFアプリで最初のMVVMを実装してみてください。WinFormsに基づく古いアプリがあり、このプロジェクトのロジック(クラス)を使用したいと思います。

私はモデルを持っています、それはこれらのクラスで構成されています:

public class FriendData
{
//...
}

public class PingData
{
//...
}

public class PokecAccount : INotifyPropertyChanged, IDataErrorInfo
{
//...
}


public class Rp :INotifyPropertyChanged, IDataErrorInfo
{
//...
}


public class JsonUser:INotifyPropertyChanged
{
//...
}

また、サーバーでHTTP GETおよびPOSTリクエストを送信するサービスクラス、このクラスは次のインターフェイスを実装します。

interface IPokecService
{
    bool LogOn(string nick, string password);
    bool LogOff();
    JsonUser CreateJsonUser(string nick);
    void Ping();
    void IbRp();
    bool SendRp(Rp rp);
    Rp LoadRp();
}


public class PokecService:IPokec
{
 public PokecAccount account;
 public PingData pingData;

 //impelent interface IPokec

}

WPF Model-View-ViewModelToolkit0.1のDelegateCommandを使用してみます。

ビューで問題があります。しかし、私の問題は、ViewModelのクラスPokecServiceのメソッドをどのように「ラップ」するかです。

LogOnメソッドの例。

まず、ビューを作成します。

StartUpWindow.xaml

    <StackPanel Grid.Row="1" Margin="12,12,12,12">
        <Label Name="lbAzetID" Content="AzetID" Style="{StaticResource lb1}"></Label>
        <TextBox Name="tbAzetID" Style="{StaticResource tb1}">
            <TextBox.Text>
                <Binding Path="AzetId" Mode="OneWayToSource" UpdateSourceTrigger="PropertyChanged">
                </Binding>
            </TextBox.Text>
        </TextBox>
        <Label Name="lbRegistration" Content="Registrácia" Style="{StaticResource lb1}">
            <Label.ToolTip>
                <StackPanel>
                    <TextBlock FontWeight="Bold">Registrácia</TextBlock>
                    <TextBlock>Nemáte vlástené AzetID, registrujte sa tu!</TextBlock>
                </StackPanel>
            </Label.ToolTip>
        </Label>
        <Label Name="lbPassword" Content="Heslo" Style="{StaticResource lb1}" ></Label>
        <TextBox Name="tbPassword" Style="{StaticResource tb1}">
            <TextBox.Text>
                <Binding Path="Password" Mode="OneWayToSource" UpdateSourceTrigger="PropertyChanged">
                </Binding>
            </TextBox.Text>
        </TextBox>
        <Label Name="lbForgetPassword" Content="Zabudli ste heslo?" Style="{StaticResource lb1}">
            <Label.ToolTip>
                <StackPanel>
                    <TextBlock FontWeight="Bold">Zabudli ste svoje heslo?</TextBlock>
                    <TextBlock>Nechajte si ho zaslať na Váš email.</TextBlock>
                </StackPanel>
            </Label.ToolTip>
        </Label>
    </StackPanel>
    <Button Name="LogOn"
            Command="{Binding LogOnCommand}"
            Content="Prihlásiť" 
            Width="100" 
            Height="25" 
            VerticalAlignment="Center"
            Grid.Row="2" />

2つのtexboxと1つのボタンのみで構成されています。ViewModelのプロパティでボタンをコンマでバインドします。

VieModel StartUpViewModel.cs

このクラスでは、DelegateCommandのクラスPokecServiceのラップメソッドが必要です。これらはUIコントロールにバインドされます。

public class StartUpViewModel
{

    private string _name = "KecMessanger";
    private string _password = "KecMessanger";
    private PokecService _pokecService;

    public StartUpViewModel()
    {
        _pokecService=new PokecService();
    }

    DelegateCommand _logOnCommand;

    public ICommand LogOnCommand
    {
        get
        {
            if(_logOnCommand==null)
            {
                _logOnCommand=new DelegateCommand(LogOn,CanLogOn);
            }
            return _logOnCommand;
        }
    }

    private void LogOn()
    {
        //In this method I need to call method LogOn from calss PokecService _pokecService.LogOn(_name,_password)          
        //if loging is success I need create another window - view and close this window
        //somehing like this:
        if (_pokecService.LogOn(_name, _password))
        {
            var newViewWindow = new AnotherView();
            //close StartUpView (its typeof window) but I don’t know how
            AnotherView.Show();
        }           
    }

    private bool CanLogOn()
    {
        return true;
    }
}

私の質問は次のとおりです。

  1. ViewModelのプロパティのビューからUIコントロールをバインドできますが、問題ありません。DelegateCommadを使用してViewModelのクラスPokecServiceのメソッドを「ラップ」するのは良い方法ですか?

  2. ViewModelで新しいウィンドウ/ビューを作成するのは良いことですか?ViewModelで実際のView(Window)を閉じるにはどうすればよいですか?

  3. この問題に最も適した解決策は何ですか?

メソッドをDelegateCommand変数でラップし、これらの変数プロパティを作成するだけで、このプロパティはUIコントロールにバインドされると思いますが、MVVMの初心者です。私はいくつかの記事を読みましたが、それらは非常に単純なデモしか示していません。

よろしくお願いします。英語でごめんなさい。

4

1 に答える 1

1
  1. コマンドは、MVVMのビューとビューモデルの間で通信するために作成されます。PokecServiceをViewModelから抽象化するには、依存性注入を使用することをお勧めします(私自身はMEFを使用しています)。
  2. ビューに依存性注入を提供するので、クラスのテストが容易になります。必要に応じてインターフェイスを作成し、タイプをどこでも置き換えることなく、サービスクラスの実際の実装を置き換えることができるようにします。
  3. DIコンテナを使用します。

注:私はWAFをMEFと一緒にMVVMフレームワークとして使用しており、それは魅力のように機能します。

簡単な例:

[Export(typeof(IService))]
public class Service : IService
{
}

public interface IService
{
  void Method();
}

[Export]
public class Consumer
{
  private readonly IService _Service;

  [ImportingConstructor]
  public Consumer(IService service)
  {
    ser = service;
  }


  public void DoStuff()
  {
    //stuff
    _Service.Method();
  }
}

スタートアップメソッド(つまりApplication.OnStartup):

var catalog = new AssemblyCatalog(System.Reflection.Assembly.GetExecutingAssembly()); //insert the assembly defining the mentioned classes above
var container = new CompositionContainer(catalog);
container.ComposeParts(this);

Consumer c = container.GetExportedValue<Consumer>();
c.DoStuff();
于 2010-11-20T19:06:33.947 に答える