2012-02-14 8 views
1

XAMLと.CSのサンプルコードが添付されているので、その内容はすべてわかります。私は少なくとも10個のボタンを持つMainWindowを持っています(メディア要素は1つ、プレイリストは2つ、アプリケーション自体は3つです)。 .csファイルのメソッドの数十を持つファイル。私はこれらを分離し、まだコントロールとメソッドの間の通信を維持する方法は考えている。別の.csファイル(WPF)でコントロールの動作を定義する方法はありますか。

を、私はこのようにメインウィンドウを持っている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> 

とメインウィンドウに.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) 
    {} 
} 
} 

答えて

1

ロジックとプレゼンテーションを分離する良い方法は、MVVMデザインパターンです。ここにチュートリアルがあります: http://msdn.microsoft.com/en-us/magazine/dd419663.aspx

+0

それは唯一の方法ですか?それは、私が最初から学ぶためにWPFの横にもう一つのことがあることを知って怖いです:) – HomeMade

+1

良い、しかし、複雑な、ここでは単純な、[本当に素晴らしい](http://jmorrill.hjtcentral.com/Home/tabid/ 428/EntryId/432/MVVM-for-Tarded-Folks-Like-Me-MVVM-What-Me-Me-Me.aspx) – HomeMade

関連する問題