因此,我试图为我正在创建一个应用程序,它有一个文本块和图像,可以在绑定代码中更改的自定义控件。到目前为止,它主要起作用,一旦我经历了一个小问题:当我去调试应用程序时,最初图像不显示在控件上,但是当我进入控件的模板并将“模板”从“ 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
“它工作得很好”。好的,你为什么在这里寻求帮助? –