admin管理员组文章数量:1297047
I want to display a community toolkit popup from a view model. The type of popup I want is a message box where I can pass a string variable that is the message I want displayed. I followed this Learn Popup Article from Microsoft, and I was able to build my popup and data bind the popup to a view model without any trouble. My problem comes when I try to display the popup from a different view model. Here is what I have so far:
Popup XAML
<ctm:Popup xmlns=";
xmlns:ctm=";
xmlns:vwmod="clr-namespace:LockAndKeyMaui.ViewModels"
xmlns:x=";
x:Class="LockAndKeyMaui.MsgBox"
x:DataType="vwmod:MsgViewModel"
CanBeDismissedByTappingOutsideOfPopup="False">
<VerticalStackLayout
WidthRequest="300" HeightRequest="200"
BackgroundColor="Blue">
<Label
Text="{Binding Msg}"
TextColor="Yellow"
VerticalOptions="Center"
HorizontalOptions="Center" />
</VerticalStackLayout>
</ctm:Popup>
Popup C#
using CommunityToolkit.Maui.Views;
using LockAndKeyMaui.ViewModels;
namespace LockAndKeyMaui;
public partial class MsgBox : Popup
{
readonly string MsgTxt;
public MsgBox(MsgViewModel msgvw)
{
InitializeComponent();
BindingContext = msgvw;
}
}
Popup View Model
using CommunityToolkit.Maui.Core;
using System.ComponentModel;
namespace LockAndKeyMaui.ViewModels
{
public class MsgViewModel : INotifyPropertyChanged
{
private string? msg;
readonly IPopupService popupService;
public string Msg
{
get => msg!;
set
{
msg = value;
OnPropChg(nameof(Msg));
}
}
public MsgViewModel(IPopupService popupService)
{
this.popupService = popupService;
}
public event PropertyChangedEventHandler? PropertyChanged;
private void OnPropChg(string prName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(prName));
}
}
}
Now in a new view model I'm trying to use the command that the article shows which is this.popupService.ShowPopup<UpdatingPopupViewModel>(onPresenting: viewModel => viewModel.Name = "Shaun");
but I'm just trying to replace UpdatingPopupViewModel with UpdatingMsgViewModel and that's one of the red underlines I get. I replaced the line with this.popupService.ShowPopup<MsgViewModel>(onPresenting: viewModel => viewModel.Msg = "This is a test.");
and that's where the variable Msg does not exist.
I have the popup registered with builder.Services.AddTransient<MsgBox, MsgViewModel>();
in the MauiProgram.cs file.
So what am I missing? Thanks for the help.
本文标签: cI cannot display a popup from a view modelStack Overflow
版权声明:本文标题:c# - I cannot display a popup from a view model - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741648480a2390331.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论