所以,我纠正了几乎所有的错误,但是如果我能说的话,这里有一个更重要的内容。所以我创建了一个名为MessageBoxEx.xaml
的xaml,带有“Loaded name: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;
}
}
}
}
跳,名字变红了,VS对我说:含糊不清,已经同名了。甚至对于InitializeComponent(),IComponentConnector.Connect和_contentLoaded。
显然,当我更改类的名称时,此错误消失,但如果我这样做,则.xaml的类将变为false。
我还想指出,如果我更改类的名称,xaml.cs的对象'Loaded =“Window_Loaded”'不再被xaml.cs引用,那么它就会变为红色。
那么,我该怎么做才能纠正这个问题呢?是否可以在MessageBoxEx.xaml
的{{1}}中引用现有名称?我还添加了URI方法,以防我错误地输入相同的名称。
你如何进行?
答案 0 :(得分: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);
}
}