C#XAML代码问题
本文关键字:问题 代码 C#XAML | 更新日期: 2023-09-27 18:03:13
我正试图完成这个程序,但我有一些错误无法消除。我一直在关注我的C#书,我似乎找不到遗漏的步骤。我将发布XAML代码和常规代码。
我的错误如下。。。
Warning 3 Unreachable code detected
Error 1 The type or namespace name 'memberSince' could not be found (are you missing a using directive or an assembly reference?)
Error 4 The type or namespace name 'Member' could not be found (are you missing a using directive or an assembly reference?)
Error 5 The type or namespace name 'Member' could not be found (are you missing a using directive or an assembly reference?)
Error 6 Delegate 'System.Threading.ParameterizedThreadStart' does not take 0 arguments
Error 2 'BellRingers.MainWindow.memberSince' is a 'field' but is used like a 'type'
XAML:
<Window x:Class="BellRingers.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Middleshire Bell Ringers Association - Members" Height="470" Width="600" Closing="Window_Closing">
<Window.Resources>
<Style x:Key="bellRingersFontStyle" TargetType="Control">
<Setter Property="FontFamily" Value="Comic Sans MS" />
</Style>
<Style x:Key="bellRingersStyle" TargetType="Control">
<Setter Property="Background" Value="Gray" />
<Setter Property="Foreground" Value="White" />
<Setter Property="FontFamily" Value="Comic Sans MS" />
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="Blue" />
</Trigger>
</Style.Triggers>
</Style>
<ContextMenu x:Key="textBoxMenu" Style="{StaticResource bellRingersFontStyle}" >
<MenuItem Header="Clear Name" Name="clearName" Click="clearName_Click" />
</ContextMenu>
</Window.Resources>
<Grid>
<Image Panel.ZIndex="0" Margin="0,0,0,0" Name="image1" >
<Image.Source>
<BitmapImage UriSource="bell.gif" />
</Image.Source>
</Image>
<Label Content="First Name" Height="28" HorizontalAlignment="Left" Margin="29,25,0,0" Name="label1" VerticalAlignment="Top" Width="75" Style="{StaticResource bellRingersFontStyle}" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="121,25,0,0" Name="firstName" ContextMenu="{StaticResource textBoxMenu}" VerticalAlignment="Top" Width="175" Style="{StaticResource bellRingersStyle}" IsEnabled="False" />
<Label Content="Last Name" Height="28" HorizontalAlignment="Left" Margin="302,25,0,0" Name="label2" VerticalAlignment="Top" Width="75" Style="{StaticResource bellRingersFontStyle}" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="380,25,0,0" Name="lastName" ContextMenu="{StaticResource textBoxMenu}" VerticalAlignment="Top" Width="175" Style="{StaticResource bellRingersStyle}" IsEnabled="False" />
<Label Content="Tower" Height="28" HorizontalAlignment="Left" Margin="29,72,0,0" Name="label3" VerticalAlignment="Top" Width="75" Style="{StaticResource bellRingersFontStyle}" />
<ComboBox Height="23" HorizontalAlignment="Left" Margin="121,72,0,0" Name="towerNames" VerticalAlignment="Top" Width="275" Style="{StaticResource bellRingersFontStyle}" IsEnabled="False" />
<CheckBox Content="Captain" Height="23" HorizontalAlignment="Left" Margin="420,72,0,0" Name="isCaptain" VerticalAlignment="Top" Width="75" Style="{StaticResource bellRingersFontStyle}" IsEnabled="False" />
<Label Content="Member Since" Height="28" HorizontalAlignment="Left" Margin="29,134,0,0" Name="label4" VerticalAlignment="Top" Width="90" Style="{StaticResource bellRingersFontStyle}" />
<DatePicker Height="23" HorizontalAlignment="Left" Margin="121,134,0,0" Name="memberSince" VerticalAlignment="Top" Width="275" IsEnabled="False" />
<GroupBox Header="Experience" Height="200" HorizontalAlignment="Left" Margin="29,174,0,0" Name="yearsExperience" VerticalAlignment="Top" Width="258" Style="{StaticResource bellRingersFontStyle}" IsEnabled="False">
<StackPanel Margin="0,0,0,0" Name="stackPanel1">
<RadioButton Content="Up to 1 year" Height="16" Name="novice" Width="120" Margin="0, 10, 0, 0" />
<RadioButton Content="1 to 4 years" Height="16" Name="intermediate" Width="120" Margin="0, 20, 0, 0" />
<RadioButton Content="5 to 9 years" Height="16" Name="experienced" Width="120" Margin="0, 20, 0, 0" />
<RadioButton Content="10 or more" Height="16" Name="accomplished" Width="120" Margin="0, 20, 0, 0" />
</StackPanel>
</GroupBox>
<ListBox Height="200" HorizontalAlignment="Left" Margin="310,174,0,0" Name="methods" VerticalAlignment="Top" Width="245" Style="{StaticResource bellRingersFontStyle}" IsEnabled="False" />
<Button Content="Clear" Height="23" HorizontalAlignment="Left" Margin="313,378,0,0" Name="clear" VerticalAlignment="Top" Width="75" Style="{StaticResource bellRingersStyle}" Click="clear_Click" IsEnabled="False" />
<DockPanel Height="100" HorizontalAlignment="Stretch" Margin="0" Name="dockPanel1" VerticalAlignment="Top" Width="Auto">
<Menu Style="{StaticResource bellRingersFontStyle}" Height="23" Name="menu1" Width="Auto" DockPanel.Dock="Top" VerticalAlignment="Top">
<MenuItem Header="_File">
<MenuItem Header="_New Member" Name="newMember" Click="newMember_Click" >
<MenuItem.Icon>
<Image Source="Face.bmp" />
</MenuItem.Icon>
</MenuItem>
<MenuItem Header="_Save Member Details" Name="saveMember" IsEnabled="False" Click="saveMember_Click">
<MenuItem.Icon>
<Image Source="Note.bmp" />
</MenuItem.Icon>
</MenuItem>
<Separator />
<MenuItem Header="E_xit" Name="exit" Click="exit_Click" />
</MenuItem>
<MenuItem Header="_Help">
<MenuItem Header="_About Middleshire Bell Ringers" Name="about" Click="about_Click" >
<MenuItem.Icon>
<Image Source="Ring.bmp" />
</MenuItem.Icon>
</MenuItem>
</MenuItem>
</Menu>
</DockPanel>
<StatusBar Height="23" HorizontalAlignment="Stretch" Margin="0" Name="status" VerticalAlignment="Bottom" Width="Auto" Style="{StaticResource bellRingersFontStyle}" />
</Grid>
代码背后:
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private string[] towers = { "Great Shevington", "Little Mudford",
"Upper Gumtree", "Downley Hatch" };
private string[] ringingMethods = {"Plain Bob", "Reverse Canterbury",
"Grandsire", "Stedman", "Kent Treble Bob", "Old Oxford Delight",
"Winchendon Place", "Norwich Suprise", "Crayford Little Court" };
private ContextMenu windowContextMenu = null;
public MainWindow()
{
InitializeComponent();
this.Reset();
MenuItem saveMemberMenuItem = new MenuItem();
saveMemberMenuItem.Header = "Save Member Details";
saveMemberMenuItem.Click += new RoutedEventHandler(saveMember_Click);
MenuItem clearFormMenuItem = new MenuItem();
clearFormMenuItem.Header = "Clear Form";
clearFormMenuItem.Click += new RoutedEventHandler(clear_Click);
windowContextMenu = new ContextMenu();
windowContextMenu.Items.Add(saveMemberMenuItem);
windowContextMenu.Items.Add(clearFormMenuItem);
}
public void Reset()
{
firstName.Text = String.Empty;
lastName.Text = String.Empty;
towerNames.Items.Clear();
foreach (string towerName in towers)
{
towerNames.Items.Add(towerName);
}
towerNames.Text = towerNames.Items[0] as string;
methods.Items.Clear();
CheckBox method = null;
foreach (string methodName in ringingMethods)
{
method = new CheckBox();
method.Margin = new Thickness(0, 0, 0, 10);
method.Content = methodName;
methods.Items.Add(method);
}
isCaptain.IsChecked = false;
novice.IsChecked = true;
memberSince.Text = DateTime.Today.ToString();
}
private void clear_Click(object sender, RoutedEventArgs e)
{
this.Reset();
}
//private void add_Click(object sender, RoutedEventArgs e)
//{
// string nameAndTower = String.Format(
// "member name: {0} {1} from the tower at {2} rings the following methods:",
// firstName.Text, lastName.Text, towerNames.Text);
// StringBuilder details = new StringBuilder();
// details.AppendLine(nameAndTower);
// foreach (CheckBox cb in methods.Items)
// {
// if (cb.IsChecked.Value)
// {
// details.AppendLine(cb.Content.ToString());
// }
// }
// MessageBox.Show(details.ToString(), "Member Information");
//}
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
MessageBoxResult key = MessageBox.Show(
"Are you sure you want to quit",
"Confirm",
MessageBoxButton.YesNo,
MessageBoxImage.Question,
MessageBoxResult.No);
e.Cancel = (key == MessageBoxResult.No);
}
private void newMember_Click(object sender, RoutedEventArgs e)
{
this.Reset();
saveMember.IsEnabled = true;
firstName.IsEnabled = true;
lastName.IsEnabled = true;
towerNames.IsEnabled = true;
isCaptain.IsEnabled = true;
memberSince.IsEnabled = true;
yearsExperience.IsEnabled = true;
methods.IsEnabled = true;
clear.IsEnabled = true;
this.ContextMenu = windowContextMenu;
}
private void exit_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
private void saveData(string fileName, memberSince member)
{
using (StreamWriter writer = new StreamWriter(fileName))
{
writer.WriteLine("First Name: {0}", member.FirstName);
writer.WriteLine("Last Name: {0}", member.LastName);
writer.WriteLine("Tower: {0}", member.TowerName);
writer.WriteLine("Captain: {0}", member.IsCaptain.ToString());
writer.WriteLine("Member Since: {0}", member.MemberSince.ToString());
writer.WriteLine("Methods: ");
foreach (string method in member.Methods)
{
writer.WriteLine(method);
}
Thread.Sleep(10000);
Action action = new Action(() =>
{
status.Items.Add("Member details saved");
});
this.Dispatcher.Invoke(action, DispatcherPriority.ApplicationIdle);
}
}
private void saveMember_Click(object sender, RoutedEventArgs e)
{
SaveFileDialog saveDialog = new SaveFileDialog();
saveDialog.DefaultExt = "txt";
saveDialog.AddExtension = true;
saveDialog.FileName = "Members";
saveDialog.InitialDirectory = @"C:'Users'John'Documents";
saveDialog.OverwritePrompt = true;
saveDialog.Title = "Bell Ringers";
saveDialog.ValidateNames = true;
if (saveDialog.ShowDialog().Value)
{
Member member = new Member();
member.FirstName = firstName.Text;
member.LastName = lastName.Text;
member.TowerName = towerNames.Text;
member.IsCaptain = isCaptain.IsChecked.Value;
member.MemberSince = memberSince.SelectedDate.Value;
member.Methods = new List<string>();
foreach (CheckBox cb in methods.Items)
{
if (cb.IsChecked.Value)
{
member.Methods.Add(cb.Content.ToString());
}
}
Thread workerThread = new Thread(
() => this.saveData(saveDialog.FileName, member));
workerThread.Start();
}
}
private void about_Click(object sender, RoutedEventArgs e)
{
About aboutWindow = new About();
aboutWindow.ShowDialog();
}
private void clearName_Click(object sender, RoutedEventArgs e)
{
firstName.Clear();
lastName.Clear();
}
}
struct Member
{
public string FirstName;
public string LastName;
public string TowerName;
public bool IsCaptain;
public DateTime MemberSince;
public List<string> Methods;
}
然后将saveData方法的签名更改为此。
private void saveData(string fileName, Member member)
{ ...}
这样做为我清除了所有错误。