因此,我試圖爲我正在創建一個應用程序,它有一個文本塊和圖像,可以在綁定代碼中更改的自定義控件。到目前爲止,它主要起作用,一旦我經歷了一個小問題:當我去調試應用程序時,最初圖像不顯示在控件上,但是當我進入控件的模板並將「模板」從「 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
當我正在調試。我真的不明白爲什麼這樣。那裏的文檔很少,我可以通過自定義控件的方式找到。爲什麼圖像沒有立即顯示?爲什麼改變模板會使它突然工作?
是'ImageSource'字符串還是int?你的dep prop聲明有兩個。通常不是[ImageSource'實例](https://docs.microsoft.com/en-us/uwp/api/windows.ui.xaml.media.imagesource)? –
這是一個字符串,因爲我正在替換通常爲'Image'元素的Xaml佈局中的'Source'字符串值,如模板所示。它工作得很好,當我在調試時進行編輯和撤消時。在我搜索綁定一個'Image'源代碼時,沒有提到'ImageSource'。 – Malkierian
「它工作得很好」。好的,你爲什麼在這裏尋求幫助? –