Xamarin Community Toolkit Popup MVVM 绑定问题

Xamarin Community Toolkit Popup MVVM Binding Issue

尝试在 Xamarin Forms MVVM 模式项目中使用 Xamarin Community Toolkit Popups,目标平台 IOS & Android。弹出窗口正在出现,但是我无法绑定以显示来自视图模型的 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;
}

Missing Text

在看到其余代码之前我无法判断,但我认为问题在于您如何实例化 ProviderApplicationPopup

首先你正在设置 PopupMessage 值然后你实例化 ProviderApplicationPopup 但随后可能 ProviderApplicationPopup 也实例化了具有 null PopupMessage 值的新 ViewModel。

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

P.S: 或者你也可以先实例化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;
}