【问题标题】:Unable to add PopupPage to project in Visual Studio无法将 PopupPage 添加到 Visual Studio 中的项目
【发布时间】:2019-09-19 11:48:56
【问题描述】:

我想将 Rg.Plugins.Popup 用于 Xamarin.Forms,但遗​​憾的是我无法将 PopupPage 添加到项目中。我正在使用 VIsual Studio 2017。在 AddNewItem 窗口中根本没有 PopupPage。

我尝试像这样添加ContentPage

public partial class CustomPopupPage : ContentPage
{
    public CustomPopupPage ()
    {
        InitializeComponent ();
    }
}

但每当我尝试将类型 ContentPage 更改为 PopupPage 时,我都会收到以下错误:Partial declarations of 'CustomPopupPage' must not specify different base classes.

问题是第二个部分类在自动生成的文件 CustomPopupPage.xaml.g.cs 中,我无法修改该文件,因为每次应用程序编译它都会重写该文件。

我认为我在这里遗漏了一些明显的东西,因为演示工作正常。

【问题讨论】:

    标签: xamarin.forms popup


    【解决方案1】:

    PopupPage 是 ContentPage 的子类。因此您必须添加一个新的 ContentPage 并在 xaml 和 code benind 中更改超类。

    首先,在共享项目和特定平台(iOS和Android)中从nuget安装包Rg.Plugins.Popup

    插件需要初始化。要在应用程序中使用 PopupPage,每个平台应用程序都必须初始化 Rg.Plugins.Popup。此初始化步骤因平台而异,将在以下部分进行讨论。

    iOS ->AppDelegate.cs

    public override bool FinishedLaunching(UIApplication app, NSDictionary options)
    {
       Rg.Plugins.Popup.Popup.Init();
          
       global::Xamarin.Forms.Forms.Init ();
       LoadApplication (new App ());
       return base.FinishedLaunching (app, options);
    }
    

    Android->MainActivity

    protected override void OnCreate(Bundle bundle)
    {
       base.OnCreate(bundle);
    
       Rg.Plugins.Popup.Popup.Init(this, bundle);
            
       Xamarin.Forms.Forms.Init(this, bundle);
       LoadApplication (new App ());
    }
    

    xaml

    <?xml version="1.0" encoding="utf-8" ?>
    <pages:PopupPage 
        xmlns="http://xamarin.com/schemas/2014/forms"
        xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
        xmlns:pages="clr-namespace:Rg.Plugins.Popup.Pages;assembly=Rg.Plugins.Popup"
        xmlns:animations="clr-namespace:Rg.Plugins.Popup.Animations;assembly=Rg.Plugins.Popup"
        x:Class="MyProject.MyPopupPage">
        <!--You can set an animation in the xaml file or in the csharp code behind-->
        <pages:PopupPage.Animation>
            <animations:ScaleAnimation 
                PositionIn="Center"
                PositionOut="Center"
                ScaleIn="1.2"
                ScaleOut="0.8"
                DurationIn="400"
                DurationOut="300"
                EasingIn="SinOut"
                EasingOut="SinIn"
                HasBackgroundAnimation="True"/>
        </pages:PopupPage.Animation>
        <!--You can use any elements here which are extended from Xamarin.Forms.View-->
        <StackLayout 
            VerticalOptions="Center" 
            HorizontalOptions="Center" 
            Padding="20, 20, 20, 20">
            <Label
                Text="Test"/>
        </StackLayout>
    </pages:PopupPage>
    

    在代码后面

    public partial class MyPopupPage : Rg.Plugins.Popup.Pages.PopupPage
    {
      public MyPopupPage()
      {
        InitializeComponent();
      }
    
      protected override void OnAppearing()
      {
        base.OnAppearing();
      }
    }
    

    更新

    这似乎是 vs 2017 的现有问题,在 VS 2019 上运行良好。我会将这个问题发布给产品团队。

    【讨论】:

    • 我知道怎么做,因为我有一个工作演示。问题是由于错误,我无法在后面的代码中将 ContentPage 更改为 PopupPage。你能尝试重现这个错误吗?
    • 你改变了xaml的类类型吗?
    • 是的,我做到了。问题出在 CustomPopupPage.xaml.g.cs 中,我的更改后没有更新。会不会是VS问题?
    • XF 的版本是多少?
    • 这是一个 VS 问题。我更新到 2019 年,现在可以正常使用了。
    猜你喜欢
    • 1970-01-01
    • 2012-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-14
    • 2015-06-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多