BigMarty
BigMarty

Reputation: 155

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

Upvotes: 0

Views: 3399

Answers (1)

Uzair Ali
Uzair Ali

Reputation: 540

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

P.S: 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;
}

Upvotes: 1

Related Questions