Xamarin 不使用視圖模型中的命令。事件有效,但它忽略了我的命令。我已經檢查過,命令已初始化,但仍然無法正常作業。我以為它只存在于我的作業專案中,但也存在于我一個小時前創建的測驗專案中。
我的測驗視圖:
<?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:viewmodels="clr-namespace:TestXamarinMvvm.ViewModels"
x:DataType="viewmodels:MainVM"
x:Class="TestXamarinMvvm.MainPage">
<ContentPage.BindingContext>
<viewmodels:MainVM/>
</ContentPage.BindingContext>
<StackLayout>
<Button Text="Test"
Command="{Binding TestCommand}"
HorizontalOptions="Center"
VerticalOptions="CenterAndExpand"/>
</StackLayout>
</ContentPage>
我的測驗視圖模型:
namespace TestXamarinMvvm.ViewModels
{
internal class MainVM : BaseVM
{
public MainVM()
{
TestCommand = new RelayCommand(obj =>
{
App.Current.MainPage.DisplayAlert("Work", "It is working.", "OK");
});
}
public RelayCommand TestCommand { get; }
}
}
uj5u.com熱心網友回復:
我在我這邊做了一個演示,效果很好。
這是中的代碼MainPage.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" xmlns:viewmodels="clr-namespace:App7"
x:Class="App7.MainPage">
<ContentPage.BindingContext>
<viewmodels:MainVM/>
</ContentPage.BindingContext>
<StackLayout>
<Button Text="Test"
Command="{Binding TestCommand}"
HorizontalOptions="Center"
VerticalOptions="CenterAndExpand"/>
</StackLayout>
</ContentPage>
這是 ViewModel 中的代碼MainVM.cs:
internal class MainVM
{
public MainVM()
{
TestCommand = new Command(() =>
{
App.Current.MainPage.DisplayAlert("Work", "It is working.", "OK");
});
}
public ICommand TestCommand { get; set; }
}
uj5u.com熱心網友回復:
我不確定 RelayCommand 是什么,但它似乎需要一個 obj 引數。
也許這個功能會起作用?
TestCommand = new RelayCommand(() =>
{
App.Current.MainPage.DisplayAlert("Work", "It is working.", "OK");
});
uj5u.com熱心網友回復:
我不確定中繼命令是如何作業的。但我通常將此命令用于按鈕,它適用于我。請檢查它是否有幫助。
namespace TestXamarinMvvm.ViewModels
{
internal class MainVM : BaseVM
{
public ICommand TestCommand
{
get
{
return new Command((args) =>
{
App.Current.MainPage.DisplayAlert("Work", "It is working.", "OK");
});
}
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/497394.html
標籤:C# xml xamarin xamarin.forms xamarin.uwp
上一篇:Win10桌面右鍵新增內容修改
