2017-10-16 3 views
0

私は作成しているアプリケーションのカスタムコントロールを作成しようとしています。私はアプリをデバッグするために行くとき、最初は画像がコントロールに表示されませんが、私がコントロールのテンプレートに移動し、 "テンプレート"を " TemplateBinding "を呼び出し、数秒待ってから戻すと、画像が突然コントロール上にポップアップします。ここに私のコードです。xamlがデバッグ時に変更されるまで、コントロールはバインドされたコンテンツを表示しません

テンプレート:

<Style TargetType="local:SoundButton"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="local:SoundButton"> 
       <Grid Background="#303030" Padding="10,10,10,10"> 
        <TextBlock FontSize="12" Text="{TemplateBinding Text}"/> 
        <Image x:Name="PlayingImg" Width="20" Height="20" Margin="60,60,0,0" Source="{TemplateBinding ImageSource}"/> 
       </Grid> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

コントロールクラス:

public sealed class SoundButton : Button 
{ 
    public string Path 
    { 
     get { return (string)GetValue(PathProperty); } 
     set { SetValue(PathProperty, value); } 
    } 
    public static DependencyProperty PathProperty = 
     DependencyProperty.Register("Path", typeof(string), typeof(SoundButton), new PropertyMetadata(null)); 

    public string FileName 
    { 
     get { return (string)GetValue(FileNameProperty); } 
     set { SetValue(FileNameProperty, value); } 
    } 
    public static DependencyProperty FileNameProperty = 
     DependencyProperty.Register("FileName", typeof(string), typeof(SoundButton), new PropertyMetadata(null)); 

    public string Text 
    { 
     get { return (string)GetValue(TextProperty); } 
     set { SetValue(TextProperty, value); } 
    } 
    public static DependencyProperty TextProperty = 
     DependencyProperty.Register("Text", typeof(string), typeof(SoundButton), new PropertyMetadata(null)); 

    public string ImageSource 
    { 
     get { return (string)GetValue(ImageSourceProperty); } 
     set { SetValue(ImageSourceProperty, value); } 
    } 
    public static DependencyProperty ImageSourceProperty = 
     DependencyProperty.Register("ImageSource", typeof(string), typeof(SoundButton), new PropertyMetadata(null)); 

    private MediaElement Media = null; 

    public SoundButton() 
    { 
     this.DefaultStyleKey = typeof(SoundButton); 
    } 

    public async void LoadSound() 
    { 
     Media = new MediaElement(); 
     StorageFolder folder = Windows.ApplicationModel.Package.Current.InstalledLocation; 
     var file = await folder.GetFileAsync(FileName); 
     Media.SetSource(await file.OpenAsync(FileAccessMode.Read), file.ContentType); 
     Media.MediaEnded += Stopped; 
    } 

    private void Stopped(object sender, RoutedEventArgs e) 
    { 
     Stop(); 
    } 

    private void Stop() 
    { 
     Media.Stop(); 
     ImageSource = "Assets\\Images\\Play.png"; 
    } 

    protected override void OnTapped(TappedRoutedEventArgs e) 
    { 
     if (Media == null) 
     { 
      LoadSound(); 
     } 
     base.OnTapped(e); 
     if (Media.CurrentState == MediaElementState.Playing) 
      Stop(); 
     else 
     { 
      Media.Play(); 
      ImageSource = "Assets\\Images\\Stop.png"; 
     } 
    } 
} 

奇妙なことは、私はこれをしなかった場合を除き、それは実際に持った、TextBlockの値で示された(画像が表示されないだろうということです前と同じ問題):

https://www.dropbox.com/s/bg6npa6ucqoii2w/ControlXamlUpdate.mp4?dl=0

私がデバッグしている間。私は本当になぜこれがこのようなのか分かりません。私がカスタムコントロールの方法で見つけることができるドキュメンテーションはほとんどありません。すぐに画像が表示されないのはなぜですか?そして、そのようなテンプレートを変更すると、突然動くのはなぜですか?

+0

'ImageSource'は文字列かintですか?あなたのdep prop宣言には両方があります。通常はImageSourceインスタンスではないでしょうか(https://docs.microsoft.com/en-us/uwp/api/windows.ui.xaml.media.imagesource)? –

+0

これは文字列です。なぜなら、テンプレートに表示されているように、 'Image'要素のXamlレイアウトにある' Source'文字列の値を置き換えるためです。デバッグ中に編集や取り消しを行うと、そのまま動作します。 'Image'ソースをバインドするための私の検索では、何も' ImageSource'を言及しませんでした。 – Malkierian

+0

"それはそのままでうまくいく"。 OK、なぜここで助けを求めていますか? –

答えて

0

私が最初に問題にしたのは、実際のプロパティ(TextBlockバインディングの問題を解決したもの)のタイプにDependencyPropertyの登録のtypeofの登録が一致していないということです。私の2番目はImageSourceの値がImageSourceであり、文字列ではないことを理解していなかったため、文字列の割り当てが無意味になりました。ボタンに2つの静止画像を作成し、それらを使用するように割り当てを変更しました。これは今でも完全に機能しています。ありがとうございました!

関連する問題