*.xaml.g.cs 文件显示过时警告 (CS0618)。如何在部分 class 中使用过时属性?
*.xaml.g.cs file is showing an obsolete warning (CS0618). How to use Obsolete attribute in a partial class?
我有一个自定义视图标记为过时
[Obsolete("AccordionButton is deprecated, please use HeaderButton instead.")]
通过将此属性添加到 *.xaml.cs 文件。现在我在编译 xaml.g.cs 文件中收到警告,因为它是部分 class (由 *.xaml 和 * .xaml.cs 代码隐藏文件)。
我不明白为什么我会收到警告,该视图未在其他任何地方使用。是的,我可以删除该文件,但出于好奇,他为什么不检查这是一个部分 class?我怎样才能摆脱警告并同时保留 Obsolete
属性?
编辑:
这里是 'full' 代码:
手风琴按钮。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>
手风琴按钮。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)]
所以我的项目中有一些不同之处,但我还没有弄清楚是什么以及为什么。
Obsolete attribute
将程序实体标记为不再推荐使用。每次使用标记为过时的实体随后都会生成警告或错误,具体取决于属性的配置方式。
1.Set 已过时 class
[Obsolete("AccordionButton is deprecated, please use HeaderButton instead.")]
public partial class Page1 : ContentView
{
public Page1()
{
InitializeComponent();
Page2 page2 = new Page2();
page2.method();
}
}
2.Set 方法已过时。
[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.Ignore 文件错误
`#pragma warning disable 618`
Page2 page2 = new Page2();
page2.method1();
2.Ignore 项目错误
有关禁用警告的更多信息,您可以查看 link。
更新:
Thank you for your response! You have written a nice summary here. I tried to use #pragma warning disable and #pragma warning restore, but where should I put it to not get the warning? I'll edit my question so that you can have a look. What I still don't get is why this error occurs. The Xaml is using the partial class (so it is using itself) and this is enough to cause the warning?
#pragma warning
可以启用或禁用某些警告。
用法:
#pragma warning disable warning-list
#pragma warning restore warning-list
warning-list
:
以逗号分隔的警告编号列表。 "CS" 前缀是可选的。
未指定警告编号时,disable 禁用所有警告,restore 启用所有警告。
根据您的代码,您可以在使用过时的 class 之前放置它,如下所示。
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
#pragma warning disable
AccordionButton accordionButton = new AccordionButton();
var s = accordionButton.Expand;
}
}
To be clear: the warning should be thrown, when another View is using this component (AccordionButton). It should not be shown if the xaml and the code behind file are "working together".
当其他视图使用此 class 时,也会抛出错误。
我有一个自定义视图标记为过时
[Obsolete("AccordionButton is deprecated, please use HeaderButton instead.")]
通过将此属性添加到 *.xaml.cs 文件。现在我在编译 xaml.g.cs 文件中收到警告,因为它是部分 class (由 *.xaml 和 * .xaml.cs 代码隐藏文件)。
我不明白为什么我会收到警告,该视图未在其他任何地方使用。是的,我可以删除该文件,但出于好奇,他为什么不检查这是一个部分 class?我怎样才能摆脱警告并同时保留 Obsolete
属性?
编辑:
这里是 'full' 代码:
手风琴按钮。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>
手风琴按钮。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)]
所以我的项目中有一些不同之处,但我还没有弄清楚是什么以及为什么。
Obsolete attribute
将程序实体标记为不再推荐使用。每次使用标记为过时的实体随后都会生成警告或错误,具体取决于属性的配置方式。
1.Set 已过时 class
[Obsolete("AccordionButton is deprecated, please use HeaderButton instead.")]
public partial class Page1 : ContentView
{
public Page1()
{
InitializeComponent();
Page2 page2 = new Page2();
page2.method();
}
}
2.Set 方法已过时。
[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.Ignore 文件错误
`#pragma warning disable 618`
Page2 page2 = new Page2();
page2.method1();
2.Ignore 项目错误
更新:
Thank you for your response! You have written a nice summary here. I tried to use #pragma warning disable and #pragma warning restore, but where should I put it to not get the warning? I'll edit my question so that you can have a look. What I still don't get is why this error occurs. The Xaml is using the partial class (so it is using itself) and this is enough to cause the warning?
#pragma warning
可以启用或禁用某些警告。
用法:
#pragma warning disable warning-list
#pragma warning restore warning-list
warning-list
:
以逗号分隔的警告编号列表。 "CS" 前缀是可选的。
未指定警告编号时,disable 禁用所有警告,restore 启用所有警告。
根据您的代码,您可以在使用过时的 class 之前放置它,如下所示。
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
#pragma warning disable
AccordionButton accordionButton = new AccordionButton();
var s = accordionButton.Expand;
}
}
To be clear: the warning should be thrown, when another View is using this component (AccordionButton). It should not be shown if the xaml and the code behind file are "working together".
当其他视图使用此 class 时,也会抛出错误。