首页 > 解决方案 > *.xaml.g.cs 文件显示过时警告 (CS0618)。如何在部分类中使用过时的属性?

问题描述

我有一个标记为过时的自定义视图

[Obsolete("AccordionButton is deprecated, please use HeaderButton instead.")]

通过将此属性添加到 *.xaml.cs 文件。现在我在编译时在xaml.g.cs文件中收到警告,因为它是一个部分类(由文件后面的 *.xaml 和 *.xaml.cs 代码组成)。

我不明白为什么会收到警告,该视图未在其他任何地方使用。是的,我可以删除该文件,但出于好奇,他为什么不检查这是一个部分类?如何摆脱警告并同时保留Obsolete属性?

编辑:

这是“完整”代码:

AccordionButton.xaml.cs

using MyProject.Common.CustomRenderers;
using System;
using Xamarin.Forms;

namespace MyProject.Views
{
#pragma warning disable 618
    [Obsolete("AccordionButton is deprecated, please use HeaderButton instead.")]
    public partial class AccordionButton : MultiLineButton
    {
        private bool expand = false;

        public bool Expand
        { 
            get{ return this.expand;}
            set{ this.expand = value;}
        }

        public ContentView AssociatedContent { get; set; }

        public AccordionButton()
        {

        }
    }
#pragma warning restore 618
}

AccordionButton.xaml

<?xml version="1.0" encoding="utf-8" ?>
<customViews:MultiLineButton xmlns="http://xamarin.com/schemas/2014/forms"
                         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                         x:Class="MyProject.Views.AccordionButton"
                         xmlns:customViews="clr-namespace:MyProject.Views;assembly=MyProject">

</customViews:MultiLineButton>

AccordionButton.xaml.g.cs

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.42000
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

[assembly: global::Xamarin.Forms.Xaml.XamlResourceIdAttribute("MyProject.Views.AccordionButton.xaml", "Views/AccordionButton.xaml", typeof(global::MyProject.Views.AccordionButton))]

namespace MyProject.Views {


    [global::Xamarin.Forms.Xaml.XamlFilePathAttribute("Views\\AccordionButton.xaml")]
    public partial class AccordionButton : global::MyProject.Views.MultiLineButton {

        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Xamarin.Forms.Build.Tasks.XamlG", "0.0.0.0")]
        private void InitializeComponent() {
            global::Xamarin.Forms.Xaml.Extensions.LoadFromXaml(this, typeof(AccordionButton));
        }
    }
}

如果我双击警告,他会标记这部分global::MyProject.Views.AccordionButton。警告仅在编译后出现...

当我将此与测试项目(一切正常)进行比较时,AccordionButton.xaml.g.cs看起来像这样

[assembly: global::Xamarin.Forms.Xaml.XamlResourceIdAttribute("TestObsolete.Views.AccordionButton.xaml", "Views/AccordionButton.xaml", null)]

所以我的项目中有些东西是不同的,但我还没有弄清楚是什么以及为什么。

标签: xamlxamarinobsolete

解决方案


Obsolete attribute程序实体标记为不再推荐使用的程序实体。每次使用标记为过时的实体都会随后生成警告或错误,具体取决于属性的配置方式。

1.设置过时的类

[Obsolete("AccordionButton is deprecated, please use HeaderButton instead.")]
public partial class Page1 : ContentView
{
    public Page1()
    {
        InitializeComponent();

        Page2 page2 = new Page2();
        page2.method();
    }
}

在此处输入图像描述

2.设置过时的方法。

[Obsolete("AccordionButton is deprecated, please use HeaderButton instead.")]
public partial class Page1 : ContentView
{
    public Page1()
    {
        InitializeComponent();

        Page2 page2 = new Page2();
        page2.method();
    }
}
public partial class Page2
{
    [Obsolete("AccordionButton is deprecated, please use HeaderButton instead.",true)]
    public void method()
    {
        Console.Write("a");
    }
}

使用 IsError 属性设置过时。IsError 是一个布尔值,它向编译器指示使用 ObsoleteAttribute 属性是否应该导致它发出错误(IsError 为真)或警告(IsError 为假)。

在此处输入图像描述

如果您想在没有警告的情况下使用 Obsolete,您可以尝试以下方法。

1.忽略文件中的错误

`#pragma warning disable 618`

        Page2 page2 = new Page2();
        page2.method1();

2.忽略项目中的错误

在此处输入图像描述 有关禁用警告的更多信息,您可以查看链接。https://stackoverflow.com/a/40525222/11850033

更新:

谢谢您的答复!你在这里写了一个很好的总结。我尝试使用#pragma warning disable 和#pragma warning restore,但我应该把它放在哪里才能不收到警告?我将编辑我的问题,以便您查看。我仍然不明白为什么会发生此错误。Xaml 正在使用部分类(因此它正在使用自身),这足以引起警告吗?

#pragma warning可以启用或禁用某些警告。

用法:

#pragma warning disable warning-list
#pragma warning restore warning-list

warning-list:以逗号分隔的警告编号列表。“CS”前缀是可选的。

如果未指定警告编号,则 disable 禁用所有警告, restore 启用所有警告。

根据您的代码,您可以在使用过时的类之前放置它,如下所示。

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();
        #pragma warning disable
        AccordionButton accordionButton = new AccordionButton();
        var s = accordionButton.Expand;
    }
}

需要明确的是:当另一个视图正在使用这个组件(AccordionButton)时,应该抛出警告。如果 xaml 和文件背后的代码“一起工作”,则不应显示。

当其他视图使用此类时,也会抛出错误。

在此处输入图像描述


推荐阅读