0

尝试在 Xamarin Forms MVVM 模式项目中使用 Xamarin 社区工具包弹出窗口,目标平台 IOS 和 Android。弹出窗口正在出现,但是我无法绑定以显示来自 viewmodel 的 PopUpMessage 字符串。这是我的代码。

XAML:

<xct:Popup.BindingContext>
    <viewmodels:ProviderApplicationViewModel />
</xct:Popup.BindingContext>

<StackLayout Style="{StaticResource PopupLayout}">
    <Label Style="{StaticResource Title}" 
           Text="Application Status" />
    <BoxView Style="{StaticResource Divider}" />
    <Label Style="{StaticResource Content}" 
           Text="{Binding PopUpMessage}"
           TextColor="Black"/>
    <Button Text="OKAY"
            Style="{StaticResource ConfirmButton}"
            Clicked="Button_Clicked" />
</StackLayout>

代码背后:

public partial class ProviderApplicationPopup : Popup
{
    public ProviderApplicationPopup()
    {
        InitializeComponent();
    }

    void Button_Clicked(object sender, System.EventArgs e) => Dismiss(null);
}

视图模型:

private string popupmessage;
public string PopUpMessage 
{ 
    set { SetProperty(ref popupmessage, value); } 
    get { return popupmessage; }
}

ViewModel 弹出导航:

if (response == "True")
{
    PopUpMessage = "Your application has been submitted!";
    Navigation.ShowPopup(new ProviderApplicationPopup());
    IsBusy = false;
    return;
}

缺少文本

4

1 回答 1

1

在我看到其余代码之前我无法判断,尽管我认为问题在于你如何实例化你的ProviderApplicationPopup.

首先,您正在设置PopupMessage值,然后您实例化ProviderApplicationPopup,但随后可能ProviderApplicationPopup也用空PopupMessage值实例化新的 ViewModel。

ProviderApplicationPopup所以你可以在实例化 时直接传递字符串,new ProviderApplicationPopup("Your application has been submitted!")然后在构造函数中设置值

PS:或者您也可以先实例化 ViewModel 然后将其传递给ProviderApplicationPopup然后绑定它。

编辑:

var viewModel = new ProviderApplicationViewModel()
{
    PopUpMessage = "Your application has been submitted!";
}
Navigation.ShowPopup(new ProviderApplicationPopup(viewModel));

//------------- In ProviderApplicationPopup -------------

public ProviderApplicationPopup(ProviderApplicationViewModel viewModel)
{
    BindingContext = viewModel;
}
于 2021-05-14T18:15:33.277 回答