2012-01-10 12 views
0

自分自身のための練習としてサウンドボードを作っています。私は、XMLファイルからURIを解析し、それらを単一のメディア要素にバインドしたいと考えています。さまざまなオーディオファイルをトリガーする8つのボタンがあります。私は1ページしか持っていないので、NavigationContext.QueryStringを使用して属性IDをLINQのクエリ文字列として渡すことはできません。どのボタンを押すかによって属性を変更するにはどうすればよいですか?Linq toクリックイベントに属性を割り当てる

ここでは、コードです:

オーディオクラス:

public class AudioClass 
{ 
    string audio; 

    public string Audio 
    { 
     get { return audio; } 
     set { audio = value; } 
    } 

} 

コード:

public partial class MainPage : PhoneApplicationPage 
{ 
    string name = "C"; 

    public MainPage() 
    { 
     InitializeComponent();  
    } 

    protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) 
    { 
     XDocument audioPlayer = XDocument.Load("Audio.xml"); 

     var aani = (from audio in audioPlayer.Descendants("Note") 
        where audio.Attribute("id").Value == name 
        select new AudioClass 
        { 
         Audio = (string)audio.Element("url").Value 

        }).SingleOrDefault(); 

     player.Source = new Uri(aani.Audio, UriKind.Relative); 

     base.OnNavigatedTo(e); 
    } 

    private void C_Key_Click(object sender, RoutedEventArgs e) 
    { 
     player.Play(); 
    } 

    private void D_Key_Click(object sender, RoutedEventArgs e) 
    { 
     player.Play(); 
    } 

とXAML:

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="41,-8,-17,8"> 
     <Button x:Name="C_key" Content="" HorizontalAlignment="Left" Height="220" Margin="8,0,0,8" Style="{StaticResource C}" VerticalAlignment="Bottom" Width="75" Click="C_Key_Click"/> 
     <Button x:Name="D_key" Content="" HorizontalAlignment="Left" Height="220" Margin="87,0,0,8" Style="{StaticResource D}" VerticalAlignment="Bottom" Width="75" Click="D_Key_Click"/> 
     <Button x:Name="E_key" Content="" HorizontalAlignment="Left" Height="220" Margin="166,0,0,8" Style="{StaticResource E}" VerticalAlignment="Bottom" Width="75" Click="E_Key_Click"/> 
     <Button x:Name="F_key" Content="" HorizontalAlignment="Left" Height="220" Margin="245,0,0,8" Style="{StaticResource F}" VerticalAlignment="Bottom" Width="75" d:LayoutOverrides="Width" Click="F_Key_Click"/> 
     <Button x:Name="G_key" Content="" Height="220" Margin="324,0,305,8" Style="{StaticResource G}" VerticalAlignment="Bottom" Click="G_Key_Click"/> 
     <Button x:Name="A_key" Content="" HorizontalAlignment="Right" Height="220" Margin="0,0,226,8" Style="{StaticResource A}" VerticalAlignment="Bottom" Width="75" Click="A_Key_Click"/> 
     <Button x:Name="B_key" Content="" HorizontalAlignment="Right" Height="220" Margin="0,0,147,8" Style="{StaticResource B}" VerticalAlignment="Bottom" Width="75" Click="B_Key_Click"/> 
     <Button x:Name="C2_key" Content="" HorizontalAlignment="Right" Height="220" Margin="0,0,68,8" Style="{StaticResource C2}" VerticalAlignment="Bottom" Width="75" Click="C2_Key_Click"/> 
     <MediaElement Height="120" HorizontalAlignment="Left" Margin="8,6,0,0" Name="player" VerticalAlignment="Top" Width="160" Source="{Binding Audio}" Volume="1" AutoPlay="False"/> 
     </Grid> 

答えて

1
  1. 例は、小道SetPlayerSource()OnNavigatedTo()から。
  2. 汎用ボタンのクリックイベントハンドラでSetPlayerSource()を使用します。答えるため

    protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) 
    { 
        SetPlayerSource(); 
    
        base.OnNavigatedTo(e); 
    } 
    
    private void SetPlayerSource() 
    { 
        XDocument audioPlayer = XDocument.Load("Audio.xml"); 
    
        var aani = (from audio in audioPlayer.Descendants("Note") 
           where audio.Attribute("id").Value == name 
           select new AudioClass 
           { 
            Audio = (string)audio.Element("url").Value 
    
           }).SingleOrDefault(); 
    
        player.Source = new Uri(aani.Audio, UriKind.Relative); 
    } 
    
    private void ButtonKey_Click(object sender, RoutedEventArgs e) 
    { 
        var buttonName = (sender as Button).Name; 
        var underscorePos = buttonName.IndexOf('_'); 
        name = buttonName.Substring(0, underscorePos); 
        SetPlayerSource(); 
        player.Play(); 
    } 
    
+0

ありがとう!しかし、上記の方法は何らかの理由でオーディオファイルをトリガーしません。それはコンパイルされますが、サウンドはありません。 –

+0

player.Sourceが有効かどうか確認してください。 – Pol

+0

よくプレーヤ.Sourceは正しいです。私がAutoPlay = "true"に変更すると、サウンドファイルが再生を開始するからです。それが間違っているときは何も起こりません。 –

関連する問題