You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MAUI ContentView调用CommunityToolkit.Maui Popup遇CS1929错误

解决MAUI中ContentView调用ShowPopupAsync的CS1929错误

问题原因

ShowPopupAsync 是针对 Page 类型的扩展方法,你的 SettingOptions 继承自 ContentView,并非 Page 实例,直接用 this(ContentView实例)调用该方法会触发类型不匹配的错误。

解决方案

需要先获取当前ContentView所在的Page实例,再通过该Page调用ShowPopupAsync:

修改后的代码

public partial class SettingOptions : ContentView
{
    private async void TextBgColorTap(object sender, TappedEventArgs e)
    {
        try
        {
            var popup = new ColorPickerPopUp();
            // 向上查找当前ContentView所属的Page
            var parentPage = this.FindParent<Page>();
            if (parentPage != null)
            {
                var result = await parentPage.ShowPopupAsync(popup);
                string res = result as string;
                // 这里添加处理返回结果的逻辑
            }
        }
        catch (Exception ex)
        {
            // 异常处理逻辑
        }
    }
}

兼容说明

如果你的MAUI版本不支持内置的FindParent方法,可以自行实现一个遍历父控件的工具方法:

public static T FindParent<T>(Element element) where T : Element
{
    while (element != null)
    {
        if (element is T parent)
            return parent;
        element = element.Parent;
    }
    return null;
}

之后替换成 var parentPage = FindParent<Page>(this); 即可正常使用。

内容的提问来源于stack exchange,提问作者Srinivas Ch

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 03:42:09