【问题标题】:how to bind popup page button to viewmodel command property in xamarin forms如何将弹出页面按钮绑定到 xamarin 表单中的 viewmodel 命令属性
【发布时间】:2019-08-08 14:43:58
【问题描述】:

我是 MVVM 和 xamarin 表单的新手。我想在 mvvm 中实现自定义弹出窗口,以下是我的代码 我的视图模型是:

public class PopupVM
{
 public ICommand CancelCommand => new Command(async () =>{await 
 Application.Current.MainPage.Navigation.PopModalAsync();});
}

我的弹出页面 Xaml:`

<pages:PopupPage.Animation>
    <animations:ScaleAnimation DurationIn="400"
    DurationOut="300"
    EasingIn="SinOut"
    EasingOut="SinIn"
    HasBackgroundAnimation="True"
    PositionIn="Center"
    PositionOut="Center"
    ScaleIn="1.2"
    ScaleOut="0.8" />
</pages:PopupPage.Animation>

<Grid Margin="12"
Padding="24"
BackgroundColor="White"
HorizontalOptions="Center"
VerticalOptions="Center">
    <Grid.RowDefinitions>
    <RowDefinition Height="*"/>
    <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
    <ColumnDefinition Width="Auto"/>
    </Grid.ColumnDefinitions>
    <StackLayout Orientation="Vertical">
        <Picker Grid.Row="0" Grid.Column="0">
            <Picker.Items>
            <x:String>....</x:String>
            </Picker.Items>
        </Picker>

    <StackLayout Orientation="Vertical" Grid.Column="0" Grid.Row="1">
        <StackLayout Orientation="Horizontal" >
            <Label Text="A"/>**strong text**
            <Picker>
            <Picker.Items>
            <x:String>Item1</x:String>
            <x:String>Item2</x:String>
            <x:String>Item3</x:String>
            </Picker.Items>
            </Picker>
        </StackLayout>

        <StackLayout Orientation="Horizontal" >
            <Label Text="B"/>
            <Picker>
            <Picker.Items>
            <x:String>....</x:String>
            </Picker.Items>
            </Picker>
        </StackLayout>

        <StackLayout Orientation="Horizontal" >
            <Label Text="C"/>
            <Picker>
            <Picker.Items>
            <x:String>....</x:String>
            </Picker.Items>
            </Picker>
        </StackLayout>
    </StackLayout>
        <Button Text="Ok"/>
        <Button Text="Cancel" x:Name="cnclBtn" />
    </StackLayout>
</Grid>

我的 Popupxaml.cs 页面是:

public partial class MyPopupPage 
{
static PopupVM vm;
public MyPopupPage ()
{
    InitializeComponent ();

    if (vm==null)
    { vm = new PopupVM(); }
    BindingContext = vm;
}
}

我没有得到我犯错的地方。而且我需要自定义弹出窗口而不是默认显示警报

【问题讨论】:

  • 什么是pages:PopupPage 你到底在用什么弹窗插件?
  • 请在有人关闭之前详细说明您的问题
  • 你想将CancelCommand绑定到名为cnclBtn的Button吗?

标签: mvvm xamarin.forms


【解决方案1】:
<Button Text="Cancel" x:Name="cnclBtn" Command="{binding CancelCommand}" />

有关命令和绑定的更多信息,请转到this

【讨论】:

  • 我试过了。它在这个类中不起作用,因为 MyPopupPage 不继承视图或页面。
猜你喜欢
  • 2020-05-28
  • 2017-01-15
  • 2012-09-03
  • 2017-01-18
  • 2019-01-03
  • 1970-01-01
  • 2017-04-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多