我附上了我的XAML和.CS示例代码,以便您可以看到它的全部内容。我有MainWindow至少有10个按钮(一个用于媒体元素,第二个用于播放列表,第三个用于应用程序本身。此外,我在所有这些提到的方法中都使用了辅助方法。
只有一个带有几十种方法的.cs文件真的很难看。我不知道如何分离这些并仍然保持控件和方法之间的通信。
我在XAML中有这样的主窗口
<Window x:Class="Modulated.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="194" Width="525">
<Grid>
<Button Content="Sort Asc" Height="23" HorizontalAlignment="Left" Margin="10,12,0,0" Name="button3" VerticalAlignment="Top" Width="75" Click="sort_asc" />
<Button Content="Sort Desc" Height="23" HorizontalAlignment="Left" Margin="12,42,0,0" Name="button4" VerticalAlignment="Top" Width="75" />
<ListBox Height="131" HorizontalAlignment="Left" Margin="99,12,0,0" Name="listBox1" VerticalAlignment="Top" Width="70" >
<ListBoxItem>1</ListBoxItem>
<ListBoxItem>2</ListBoxItem>
<ListBoxItem>3</ListBoxItem>
</ListBox>
<MediaElement Height="131" HorizontalAlignment="Left" Margin="187,12,0,0" Name="mediaElement1" VerticalAlignment="Top" Width="165" />
<Button Content="Play" Height="23" HorizontalAlignment="Left" Margin="372,12,0,0" Name="btnPlay" VerticalAlignment="Top" Width="75" Click="play" />
<Button Content="Stop" Height="23" HorizontalAlignment="Left" Margin="372,42,0,0" Name="btnStop" VerticalAlignment="Top" Width="75" Click="stop" />
<Button Content="Next" Height="23" HorizontalAlignment="Left" Margin="372,71,0,0" Name="btnNext" VerticalAlignment="Top" Width="75" Click="next" />
</Grid>
和MainWindow.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace Modulated
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void play(object sender, RoutedEventArgs e)
{
mediaElement1.Play();
}
private void stop(object sender, RoutedEventArgs e)
{
mediaElement1.Stop();
}
private void next(object sender, RoutedEventArgs e)
{}
private void sort_asc(object sender, RoutedEventArgs e)
{}
}
}
答案 0 :(得分:1)
分离逻辑和表示的一种好方法是MVVM设计模式。你可以在这里找到一个教程: http://msdn.microsoft.com/en-us/magazine/dd419663.aspx