我對 MVVM 模式和 Xamarin 平臺都是新手。我試圖通過使用視圖模型來創建一個可系結的介面。我從一個非常簡單的任務開始:從用戶那里獲取 Entry 輸入,并在單擊按鈕后將其發送到標簽文本。
ViewModel ---> 命令 ---> EntryCommand.cs
namespace HelloWorld.ViewModel.Commands
{
public class EntryCommand : ICommand
{
public ViewModel VM { get; set; }
public EntryCommand( ViewModel vm)
{
VM = vm;
}
public event EventHandler CanExecuteChanged;
public bool CanExecute(object parameter)
{
return true;
}
public void Execute(object parameter)
{
VM.ChangeLabelText();
}
}
}
視圖模型.cs
namespace HelloWorld.ViewModel
{
public class ViewModel: INotifyPropertyChanged
{
public EntryCommand EntryCommand { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
public ViewModel()
{
EntryCommand = new EntryCommand(this);
}
private void OnPropertyChanged(string PropertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(PropertyName));
}
public string LabelText { get; set; }
public string Name
{
get { return Name; }
set
{
Name = value;
OnPropertyChanged("Name");
}
}
public void ChangeLabelText()
{
if(Name!=null)
{
LabelText = Name;
}
}
}
}
可系結的 Xaml 檔案:
主頁.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="HelloWorld.MainPage"
xmlns:ViewModel="clr-namespace:HelloWorld.ViewModel" x:DataType="ViewModel:ViewModel">
<ContentPage.Resources>
<ViewModel:ViewModel x:Key="vm"/>
</ContentPage.Resources>
<StackLayout BindingContext="{StaticResource vm}" BackgroundColor="AliceBlue"
Margin="15">
<Label Text="{Binding Name, Mode=TwoWay}"/>
<Entry Placeholder="Enter Your Name" Text="{Binding Name, Mode=TwoWay}"/>
<Button Text="Enter" Command="{Binding EntryCommand}"/>
</StackLayout>
</ContentPage>
現在,我沒有收到任何錯誤,但模擬器在構建 UI 之前一直停止:
在此處輸入影像描述
uj5u.com熱心網友回復:
我認為您使用 ViewModel 不正確。
根據您的需要,無需創建EntryCommand: ICommand.
你可以這樣做:
public ICommand EntryCommand => new Command(DoSomething);
public void DoSomething() {
// do some thing here
}
此外,使用Name也不正確,需要創建另一個變數(例如private string _name;)
private string _name;
public string Name
{
set { SetProperty(ref _name, value); }
get { return _name; }
}
您可以在此處參考完整的示例代碼:
public class MyViewModel: INotifyPropertyChanged
{
public ICommand EntryCommand => new Command(DoSomething);
public MyViewModel() {
Name = "abc123";
}
public void DoSomething() {
// do some thing here
}
public string LabelText { get; set; }
private string _name;
public string Name
{
set { SetProperty(ref _name, value); }
get { return _name; }
}
public void ChangeLabelText()
{
if (Name != null)
{
LabelText = Name;
}
}
bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
{
if (Object.Equals(storage, value))
return false;
storage = value;
OnPropertyChanged(propertyName);
return true;
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string PropertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(PropertyName));
}
}
在xaml中使用時,可以設定BindingContext如下:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:testapp0211="clr-namespace:TestApp0211"
x:Class="TestApp0211.MainPage">
<ContentPage.BindingContext>
<testapp0211:MyViewModel />
</ContentPage.BindingContext>
<StackLayout BackgroundColor="AliceBlue"
Margin="15">
<Label Text="{Binding Name, Mode=TwoWay}"/>
<Entry Placeholder="Enter Your Name" Text="{Binding Name, Mode=TwoWay}"/>
<Button Text="Enter" Command="{Binding EntryCommand}"/>
</StackLayout>
</ContentPage>
筆記:
建議您重命名類ViewModel(例如MyViewModel)以更好地區分不同的視圖模型。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/424215.html
上一篇:無法設定選取器默認值
下一篇:BASE類中的WPF樣式
