【问题标题】:Xamarin Forms - How to have a view (ad) have a single instance over all pages?Xamarin Forms - 如何让视图(广告)在所有页面上都有一个实例?
【发布时间】:2017-05-01 03:36:17
【问题描述】:

我想使用 xamarian 表单在我的应用程序中包含横幅广告。假设我查看了广告,并且想将其放置在我的应用中,该应用在所有页面上都使用 iOS、Android 和 UWP 上的导航。

如何使所有页面的广告视图实例都相同?

最好我不想重新加载每个页面的广告视图,而是希望它存在于每个页面而不被重新加载。我认为它可能是两种方式之一,一种总是在屏幕上,如导航栏,内容呈现在添加下方,另一种方式是视图在每个页面上,但加载单个实例跨所有页面。

【问题讨论】:

  • 我不确定,但您应该使用 MasterDetailPage。在 MasterDetail 中添加公共视图。
  • 我知道以下链接对于您所要求的内容会有些繁琐,但据我所知,这是唯一的方法。 Twin Techs 的This link 谈到在另一个页面中嵌入一个页面,并允许高度可定制的导航(因为你基本上可以伪造你的导航转换)。
  • 经过几个小时的研究和测试,我无法使用原来的NavigationPage来实现一个可以嵌入其他页面的页面。一个棘手的方法是不使用NavigationPage 模板,而是创建我们自己的导航栏。有进展吗?
  • @GraceFeng-MSFT 我能够通过将 tableview 包装在堆栈布局中并首先放置我的广告视图然后放置 tableview 来获得我想要的东西。但是,这不会通过页面导航持续存在。每个页面也必须添加一个广告视图,我会得到一个新广告而不是维护旧广告。
  • @SolidSnake4444,是的,这和我想的一模一样,我们只能在xamarin表单中添加视图,这就是为什么我说不用NavigationPage模板来实现这一点。我也找不到任何更好的方法来创建像FrameLayout 用于android 中的片段膨胀或Frame 用于UWP 中的页面导航的控件,同时具有与NavigationPage 配合的能力。但是如果我们分别使用xamarin.android和xamarin.ios进行开发,应该不会有这样的问题。

标签: c# android ios xamarin xamarin.forms


【解决方案1】:

这是一个很好的方法。

转到 App.xaml 并声明以下资源:

...
<controls:AdViewControl AdUnitId="YOUR_UNIT_ID" BackgroundColor="#FFFFFF" 
                        HeightRequest="50" x:Key="AdViewControl1"/>

<ControlTemplate x:Key="AdOnFooterPage">
    <Grid BackgroundColor="#FFFFFF">
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <ContentView Content="{StaticResource AdViewControl1}" Grid.Row="1"/>
    </Grid>
</ControlTemplate>
...

接下来是将 ControlTemplate 与您要显示广告的页面链接:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design" 
             mc:Ignorable="d"
             x:Class="BdvAssistant.Views.Recharge.RechargePage"
             Title="Page 1"
             ControlTemplate="{StaticResource AdOnFooterPage}">
...

(这是可选的) 如果您的导航缓存页面,您需要取消 AdViewControl 与旧父级(ControlTemplate)的链接并将其分配给新父级。这里是 MasterDetailPage 的一个例子,下面的代码应该适应 MainPage。

public partial class MainPage : MasterDetailPage
{
    private readonly Dictionary<MenuItemType, NavigationPage> _menuPages
        = new Dictionary<MenuItemType, NavigationPage>();
    
    private readonly Dictionary<MenuItemType, ContentView> _adContentViews
        = new Dictionary<MenuItemType, ContentView>();

    private AdViewControl AdControl => (AdViewControl)Application.Current.Resources["AdViewControl1"];

    public MainPage()
    {
        InitializeComponent();
    }
    
    public async Task NavigateFromMenu(MenuItemType menu)
    {
        if (AdControl.Parent is ContentView parent)
            parent.Content = null;

        if (_menuPages.TryGetValue(menu, out var newPage))
        {
            if (_adContentViews.TryGetValue(menu, out var adParent))
                adParent.Content = AdControl;
        }
        else
        {
            switch (menu)
            {
                case MenuItemType.Dashboard:
                    newPage = new NavigationPage(new DashboardPage());
                    break;
                case MenuItemType.Security:
                    newPage = new NavigationPage(new PinManagerPage());
                    break;
                default:
                    return;
            }

            _adContentViews[menu] = AdControl.Parent as ContentView;
            _menuPages[menu] = newPage;
        }
        
        if (ReferenceEquals(Detail, newPage))
            return;
        
        Detail = newPage;

        if (MasterBehavior == MasterBehavior.Popover)
        {
            if (Device.RuntimePlatform == Device.Android)
                await Task.Delay(200);

            IsPresented = false;
        }
    }
}

【讨论】:

    猜你喜欢
    • 2017-03-20
    • 1970-01-01
    • 1970-01-01
    • 2018-08-09
    • 1970-01-01
    • 1970-01-01
    • 2017-05-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多