View Model
using System.ComponentModel;
using System.Windows.Input;
using Xamarin.Forms;
namespace MVVMPractice
{
public class XF42_CommandViewModel : INotifyPropertyChanged
{
public ICommand CmdSave { get; set; }
public string ShowUserInputMessage { get; set; }
public XF42_CommandViewModel()
{
CmdSave = new Command<string>(UserInputMessage);
}
private void UserInputMessage(string Value)
{
ShowUserInputMessage = $"使用者輸入為 {Value}";
OnPropertyChanged(nameof(ShowUserInputMessage));
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string PropertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(PropertyName));
}
}
}
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="MVVMPractice.XF42_Command">
<ContentPage.Content>
<StackLayout VerticalOptions="Center">
<Entry x:Name="entry" Text="Xamarin Command 練習"/>
<Button Text="顯示使用者輸入"
Command="{Binding CmdSave}"
CommandParameter="{Binding Source={x:Reference entry} , Path=Text}">
</Button>
<Label Text="{Binding ShowUserInputMessage}" FontSize="Large"></Label>
</StackLayout>
</ContentPage.Content>
</ContentPage>
- 練習烏龍事件
沒有留言:
張貼留言