简体   繁体   English

Xamarin Forms - Xaml 元素注入到每一页

[英]Xamarin Forms - Xaml Element injected to every page

I am looking for a way to have an element (DXPopup) injected to every page while using Xamarin Forms Shell.我正在寻找一种在使用 Xamarin Forms Shell 时将元素 (DXPopup) 注入每个页面的方法。

My initial thought would be to create a custom renderer for Tabbar and inject the popup above the tabbar.我最初的想法是为 Tabbar 创建一个自定义渲染器并在 tabbar 上方注入弹出窗口。 But I hit a blocker when my tabs are contained in a FlyoutItem.但是,当我的选项卡包含在 FlyoutItem 中时,我遇到了障碍。 I then thought about a custom renderer for ContentView and add a child element, though hit a blocker where Android wanted an android view.然后,我考虑了一个用于 ContentView 的自定义渲染器并添加了一个子元素,尽管遇到了 Android 想要 android 视图的拦截器。

How would be best to tackle this issue?如何最好地解决这个问题?

Desired outcome: DXpopup element added to every page, so we can call a common show method (by message of mediator class)期望的结果:DXpopup 元素添加到每个页面,因此我们可以调用一个通用的显示方法(通过中介类的消息)

I solved this using Jason's suggested route above:我使用上面 Jason 建议的路线解决了这个问题:

Basepage - that had a control template Control Template like: Basepage - 有一个控制模板 Control Template 如:

<ControlTemplate x:Key="BasePageTemplate">
        <StackLayout VerticalOptions="FillAndExpand">
            //your element (in my case a DXPopup
            <ContentPresenter VerticalOptions="FillAndExpand"/>
        </StackLayout>
</ControlTemplate>

Register to the binding context changing (in code behind of base page): BindingContextChanged += SetBindingContext;注册到绑定上下文更改(在基页的代码后面):BindingContextChanged += SetBindingContext;

This was to update to the consuming pages viewmodel.这是为了更新到消费页面视图模型。

Said viewmodel would have a baseViewmodel, where I registered the element injected like so:所述 viewmodel 将有一个 baseViewmodel,我在其中注册了注入的元素,如下所示:

private void SetBindingContext(object sender, EventArgs e)
    {
        if (BindingContext is not ViewModelBase baseVM) return;

        var dxPopUp = GetTemplateChild("ElementName");
        baseVM.DxPopUp = dxPopUp as DXPopup;
    }

This in turn allowed me to use the viewmodel base to interact with the element (in my case showing it)这反过来又允许我使用视图模型基础与元素交互(在我的例子中显示它)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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