gpt4 book ai didi

c# - WPF 。 NET 3.5 C# 字段已声明/具有相同名称的成员?

转载 作者:行者123 更新时间:2023-12-04 17:02:18 25 4
gpt4 key购买 nike

所以,我纠正了几乎所有的错误,但如果我能说的话,还有一个更重要的地方。所以我创建了一个名为 MessageBoxEx.xaml 的 xaml。 ,具有“加载名称:Window_Loaded”、一个名为“PartTextBlock”的文本块和一个名为“PartStackPanel”的堆栈面板。这是上面显示的 xaml 代码:

<Window x:Class="MyProject.MoLib.MessageBoxEx"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Loaded="Window_Loaded" Height="160" Width="440" WindowStyle="None" AllowsTransparency="true"
Background="Transparent" WindowStartupLocation="CenterOwner">
<Border CornerRadius="10" BorderBrush="{DynamicResource AccentBrush}" BorderThickness="3"
Background="{DynamicResource BackgroundBrush}">
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Horizontal" Height="112">
<TextBlock x:Name="PartTextBlock" Foreground="#000000" TextWrapping="Wrap"
Width="400" Height="Auto" Margin="20,34,10,6" TextAlignment="Center" VerticalAlignment="Center"/>
</StackPanel>
<StackPanel Name="PartStackPanel" Orientation="Horizontal" HorizontalAlignment="Right"/>
</StackPanel>
</Border>

然后我把这些元素的名字,以便它们被引用并实现一个功能,在 MessageBoxEx.xaml.cs ,这里是代码:
  using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Markup;
namespace MyProject.MoLib
{
public partial class MessageBoxEx : Window, IComponentConnector
{
private string FMessage = string.Empty;
private string FResult = (string)null;
private string FBtn0 = (string)null;
private string FBtn1 = (string)null;
private string FBtn2 = (string)null;
internal TextBlock PartTextBlock;
internal StackPanel PartStackPanel;
private bool _contentLoaded;

public string Btn0
{
get
{
return this.FBtn0;
}
set
{
this.FBtn0 = value;
}
}

public string Btn1
{
get
{
return this.FBtn1;
}
set
{
this.FBtn1 = value;
}
}

public string Btn2
{
get
{
return this.FBtn2;
}
set
{
this.FBtn2 = value;
}
}

public string Message
{
get
{
return this.FMessage;
}
set
{
this.FMessage = value;
}
}

public string Result
{
get
{
return this.FResult;
}
}

public TextBlock TextBlock
{
get
{
return this.PartTextBlock;
}
}
public MessageBoxEx()
{
this.InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
if (this.PartTextBlock.Inlines.Count < 1)
this.PartTextBlock.Text = this.FMessage;
this.SetupButton();
}
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
{
base.OnMouseLeftButtonDown(e);
this.DragMove();
}
private void SetupButton()
{
if (this.FBtn0 != null)
this.CreateButton("btn1", this.FBtn0);
if (this.FBtn1 != null)
this.CreateButton("btn2", this.FBtn1);
if (this.FBtn2 != null)
this.CreateButton("btn3", this.FBtn2);
Border border = new Border();
border.Width = 10.0;
this.PartStackPanel.Children.Add((UIElement)border);
}
private void button_Click(object sender, EventArgs e)
{
Button button = sender as Button;
if (button.Name == "btn1")
this.FResult = "0";
else if (button.Name == "btn2")
this.FResult = "1";
else if (button.Name == "btn3")
this.FResult = "2";
this.Close();
}

private void CreateButton(string name, string caption)
{
Button button = new Button();
button.Name = name;
button.Width = 80.0;
button.Content = (object)caption;
button.Margin = new Thickness(0.0, 15.0, 4.0, 0.0);
button.Click += new RoutedEventHandler(this.button_Click);
this.PartStackPanel.Children.Add((UIElement)button);
}

[DebuggerNonUserCode]
public void InitializeComponent()
{
if (this._contentLoaded)
return;
this._contentLoaded = true;
Application.LoadComponent((object)this, new Uri("/MoLib/MessageBoXex.xaml", UriKind.Relative));
}

[EditorBrowsable(EditorBrowsableState.Never)]
[DebuggerNonUserCode]
void IComponentConnector.Connect(int connectionId, object target)
{
switch (connectionId)
{
case 1:
((FrameworkElement)target).Loaded += new RoutedEventHandler(this.Window_Loaded);
break;
case 2:
this.PartTextBlock = (TextBlock)target;
break;
case 3:
this.PartStackPanel = (StackPanel)target;
break;
default:
this._contentLoaded = true;
break;
}
}
}
}

Hop,名字变红了,VS 对我说:暧昧,已经是同名了。即使对于 InitializeComponent()、IComponentConnector.Connect 和 _contentLoaded。

显然,当我更改类的名称时,此错误会消失,但如果这样做,.xaml 的类将变为 false。

我还想指出,如果我更改类的名称,则 xaml 的对象 'Loaded ="Window_Loaded"' 不再被 xaml.cs 引用,然后它会变成红色。

那么,我能做些什么来纠正它?是否可以在 MessageBoxEx.xaml 中引用已经存在的名称?在 MessageBoxEx.xaml.cs ?我还添加了 URI 方法,以防我错误地输入相同的名称。

你如何进行?

最佳答案

摆脱IComponentConnector interface 及其成员,并且不要定义您在代码隐藏文件的 XAML 标记中定义的字段。这些字段是为您自动生成的,因此您的代码隐藏类应如下所示:

public partial class MessageBoxEx : Window
{
private string FMessage = string.Empty;
private string FResult = (string)null;
private string FBtn0 = (string)null;
private string FBtn1 = (string)null;
private string FBtn2 = (string)null;
private bool _contentLoaded;

public string Btn0
{
get
{
return this.FBtn0;
}
set
{
this.FBtn0 = value;
}
}

public string Btn1
{
get
{
return this.FBtn1;
}
set
{
this.FBtn1 = value;
}
}

public string Btn2
{
get
{
return this.FBtn2;
}
set
{
this.FBtn2 = value;
}
}

public string Message
{
get
{
return this.FMessage;
}
set
{
this.FMessage = value;
}
}

public string Result
{
get
{
return this.FResult;
}
}

public TextBlock TextBlock
{
get
{
return this.PartTextBlock;
}
}

public MessageBoxEx()
{
this.InitializeComponent();
}

private void Window_Loaded(object sender, RoutedEventArgs e)
{
if (this.PartTextBlock.Inlines.Count < 1)
this.PartTextBlock.Text = this.FMessage;
this.SetupButton();
}
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
{
base.OnMouseLeftButtonDown(e);
this.DragMove();
}
private void SetupButton()
{
if (this.FBtn0 != null)
this.CreateButton("btn1", this.FBtn0);
if (this.FBtn1 != null)
this.CreateButton("btn2", this.FBtn1);
if (this.FBtn2 != null)
this.CreateButton("btn3", this.FBtn2);
Border border = new Border();
border.Width = 10.0;
this.PartStackPanel.Children.Add((UIElement)border);
}
private void button_Click(object sender, EventArgs e)
{
Button button = sender as Button;
if (button.Name == "btn1")
this.FResult = "0";
else if (button.Name == "btn2")
this.FResult = "1";
else if (button.Name == "btn3")
this.FResult = "2";
this.Close();
}

private void CreateButton(string name, string caption)
{
Button button = new Button();
button.Name = name;
button.Width = 80.0;
button.Content = (object)caption;
button.Margin = new Thickness(0.0, 15.0, 4.0, 0.0);
button.Click += new RoutedEventHandler(this.button_Click);
this.PartStackPanel.Children.Add((UIElement)button);
}
}

关于c# - WPF 。 NET 3.5 C# 字段已声明/具有相同名称的成员?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43458920/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com