【问题标题】:How to inherit from a Xamarin Forms base class Page with views in it?如何从其中包含视图的 Xamarin Forms 基类 Page 继承?
【发布时间】:2019-12-29 06:19:54
【问题描述】:

这让我发疯了。我正在尝试从我制作的基本 SettingsPage 继承,但它不起作用,而且我在任何地方都找不到如何做到这一点的单个示例。我发现的所有示例都涉及继承视图模型和其他非可视元素。特别是我不确定你对派生页面中的“Content=”做了什么,因为基本页面已经设置了“Content=”。关于继承的 MS doco 说你不继承构造函数,但你可以使用 :base() 从它继承。

我已经组合了一个配色方案选择器(在 amporis.xamarin.forms.colorpicker 上扩展),我想将其用作其他设置页面的基础(在我想要配色方案选择器和其他设置)。我有一个类库 CSettings,如果直接加载,CSettingsPage 可以很好地加载(其中包括 Content=SettingsGrid,所有内容都加载到其中) - 一切都是“公共的”。然后我有一个 TestBed 应用程序,并尝试创建一个 TBSettingsPage。在顶部我有

namespace TestBed {
    public class TBSettingsPage : CSettingsPage {...

(当然我已经引用了 DLL),并且我已经尝试了以下 3 种构造函数的变体...

public TBSettingsPage():base() {}
public TBSettingsPage():base() { Content=SettingsGrid; }
public TBSettingsPage() { Content=SettingsGrid; }

每次我得到...

“抛出异常:Xamarin.Forms.Platform.UAP.dll 中的“System.Runtime.InteropServices.COMException”错误 HRESULT E_FAIL 已从对 COM 组件的调用中返回。”在 Xamarin.Forms 初始化。

...这不是一个非常有用的错误消息。它基本上说“那是不对的”,但没有告诉我什么是不对的,也不告诉我如何解决(谷歌搜索会出现各种各样的情况,但这些情况都不适用于我正在尝试做的事情)。 :-(

谁能告诉我如何从已经有视图的 BasePage 派生? (我看到了一个关于在页面构建之前使其不可见的提示,然后使其可见,但这没有帮助)。或者给我举个例子来实现这样的事情?

注意:我的 UI 是 C#,而不是 XAML。如果相关的话,这也在 UWP 中。

谢谢, 唐纳德。

根据要求,这是来自 OnLaunched 的代码

受保护的覆盖无效 OnLaunched(LaunchActivatedEventArgs e) {

        Frame rootFrame = Window.Current.Content as Frame;

        // Do not repeat app initialization when the Window already has content,
        // just ensure that the window is active
        if (rootFrame == null)
        {
            // Create a Frame to act as the navigation context and navigate to the first page
            rootFrame = new Frame();

            rootFrame.NavigationFailed += OnNavigationFailed;

            Xamarin.Forms.Forms.Init(e);

            if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
            {
                //TODO: Load state from previously suspended application
            }

            // Place the frame in the current Window
            Window.Current.Content = rootFrame;
        }

        if (rootFrame.Content == null)
        {
            // When the navigation stack isn't restored navigate to the first page,
            // configuring the new page by passing required information as a navigation
            // parameter
            rootFrame.Navigate(typeof(MainPage), e.Arguments);
        }
        // Ensure the current window is active
        Window.Current.Activate();
    }

【问题讨论】:

  • 这看起来和stackoverflow.com/questions/39728469/… 是同一个问题,如果不是,请发布完整的堆栈跟踪,以便我们为您提供帮助
  • 嗨@JoshuaG。这是相同的错误消息,但原因不同(因此我对为此消息找到的许多不同结果发表评论)。调用堆栈是 ">TestBed.UWP.exe!TestBed.UWP.App.OnLaunched(Windows.ApplicationModel.Activation.LaunchActivatedEventArgs e) Line 55 C#"
  • TestBed.UWP.exe中的代码是什么!TestBed.UWP.App.OnLaunched?
  • 你能确认你的 CSettingPage 继承了 ContentPage 吗?
  • @JoshuaG 你是说 OnStart 吗?我在 app.cs 中看不到任何 OnLaunched。我自己没有添加任何东西,所以不管默认代码是什么(我尝试点击“goto definition”,但什么也没发生,所以就我所知)。

标签: c# inheritance xamarin.forms derived-class content-pages


【解决方案1】:

所以我最终发现这完全是由于 UWP 中的一个错误给了我一个红鲱鱼 - 请参阅 https://github.com/xamarin/Xamarin.Forms/issues/9335。正确的语法是我说的第一个语法 - 即public TBSettingsPage():base() {} - 但 UWP 错误阻止我看到它是正确的(因为我的应用程序仍在崩溃)。谢天谢地,这个错误现在正在处理中,现在我知道了正确的语法,我可以继续编码。 :-)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-21
    • 1970-01-01
    • 2019-05-14
    • 1970-01-01
    • 2012-10-28
    • 2012-07-21
    相关资源
    最近更新 更多