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;
}
}
私の質問は次のとおりです。
ViewModelのプロパティのビューからUIコントロールをバインドできますが、問題ありません。DelegateCommadを使用してViewModelのクラスPokecServiceのメソッドを「ラップ」するのは良い方法ですか?
ViewModelで新しいウィンドウ/ビューを作成するのは良いことですか?ViewModelで実際のView(Window)を閉じるにはどうすればよいですか?
この問題に最も適した解決策は何ですか?
メソッドをDelegateCommand変数でラップし、これらの変数プロパティを作成するだけで、このプロパティはUIコントロールにバインドされると思いますが、MVVMの初心者です。私はいくつかの記事を読みましたが、それらは非常に単純なデモしか示していません。
よろしくお願いします。英語でごめんなさい。