【问题标题】:WPF . NET 3.5 C# Field is already declared / Member with the same name?WPF 。 NET 3.5 C# 字段已声明/同名成员?
【发布时间】:2017-09-13 12:15:00
【问题描述】:

所以,我纠正了几乎所有的错误,但如果我可以说,这里还有一个更重要的问题。所以我创建了一个名为 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 方法,以防我输入相同的名称时出错。

你如何进行?

【问题讨论】:

  • 你需要修改MessageBoxEx.xaml.cs文件,而不是MessageBoxEx.g.i.cs,编译器会自动生成g.i.cs。
  • @Nikita 我不修改MessageBoxEx.g.i.cs 我修改MessageBoxEx.xaml.cs。我很难表达自己?我会改变方式,所以我指定了这个
  • ...为什么需要在你的代码中实现IComponentConnector接口?
  • @Nikita 提供对命名 XAML 元素的支持以及事件处理程序与它们的关联......我的项目在框架 3.5 中。如有必要,我会删除它......我遇到的问题的目标将很难进行任何操作
  • @Nikita ... 而IComponentConnecter 不是这些错误的问题

标签: c# .net wpf xaml namespaces


【解决方案1】:

摆脱IComponentConnector 接口及其成员,不要定义您在代码隐藏文件中的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);
    }
}

【讨论】:

  • 那个!非常感谢这个合理的回答
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-03-28
  • 2012-06-22
  • 1970-01-01
  • 1970-01-01
  • 2023-01-09
  • 2017-10-02
  • 1970-01-01
相关资源
最近更新 更多