简体   繁体   中英

Xamarin Community Toolkit Popup MVVM Binding Issue

Attempting to utilize Xamarin Community Toolkit Popups in Xamarin Forms MVVM pattern project, target platforms IOS & Android. Popups are appearing, however I cannot bind to display my PopUpMessage string from viewmodel. Here is my code.

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>

Code Behind:

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

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

ViewModel:

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

ViewModel Popup Navigation:

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

Missing Text

I can't tell until I've seen rest of the code though I think the issue is how you are instantiating your ProviderApplicationPopup .

First your are setting PopupMessage value then you instantiate ProviderApplicationPopup but then probably ProviderApplicationPopup is also instantiate new ViewModel with null PopupMessage value.

So you could pass string directly when instantiating ProviderApplicationPopup like new ProviderApplicationPopup("Your application has been submitted!") then set the value in constructor

PS: Or you can also instantiate ViewModel first then pass that to ProviderApplicationPopup and then bind it.

Edit:

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

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

public ProviderApplicationPopup(ProviderApplicationViewModel viewModel)
{
    BindingContext = viewModel;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM