The issue of ApplyTemplate

     The issue is that the ApplyTemplate method is not called in the order you are expecting.  This appears to be a known issue and there is a work-around.  If you manually call ApplyTemplate() in the Loaded event of your control, the template should be applied and the video should play.  I applied this work-around to your code along with a few other minor-tweaks and it worked correctly for me.

Also, as another suggestion, I would strongly suggest that you convert setSource to a dependency property.  You can read more about dependency properties here.

customControl.cs

public class customControl:Control
{
    private MediaElement _vidStage;

    public customControl()
    {
        this.DefaultStyleKey = typeof(customControl);
        this.Loaded += new RoutedEventHandler(customControl_Loaded);
    }

    void customControl_Loaded(object sender, RoutedEventArgs e)
    {
        this.ApplyTemplate();
    }

    public override void OnApplyTemplate()
    {
        _vidStage = base.GetTemplateChild("vidStage") as MediaElement;
    }

 ...

public Uri Source
{
    get
    {
        return _vidStage.Source;
    }
    set
    {
        if (_vidStage != null)
        {
            _vidStage.Source = value;
           //_vidStage.Play();
           // For some reason, the video wouldn't play using Play()
           // I set the AutoPlay to true and it worked.
           _vidStage.AutoPlay = true;
        }
    }
}

...

Page.xaml.cs

    public partial class Page : UserControl
    {
        public Page()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(Page_Loaded); // Setting the source after loading...
        }

        void Page_Loaded(object sender, RoutedEventArgs e)
        {
            // Please note the added forward-slash and I have set the Build Action to "Content" for the hippo.wmv file.
            g.Source = new Uri("/hippo.wmv", UriKind.Relative);
        }

 ...

 I hope this helps!

 

这是之前的完整类 public static class ComboBoxHelper { /// <summary> /// 通用初始化方法 /// </summary> /// <param name="comboBox"></param> public static void InitializeFilterComboBox(ComboBox comboBox) { comboBox.ApplyTemplate(); // 强制加载视觉树 var textBox = comboBox.Template.FindName("PART_EditableTextBox", comboBox) as TextBox; if (textBox != null) { textBox.TextChanged += (s, e) => { FilterItems(comboBox, textBox.Text); }; } } /// <summary> /// 通用过滤方法 /// </summary> /// <param name="comboBox"></param> /// <param name="input"></param> private static void FilterItems(ComboBox comboBox, string input) { try { if (comboBox.ItemsSource != null) { var view = CollectionViewSource.GetDefaultView(comboBox.ItemsSource); view.Filter = item => { // 双重空值检查 if (item == null || input == null) return false; // 安全类型转换 string itemStr = item as string ?? string.Empty; return itemStr.IndexOf(input, StringComparison.OrdinalIgnoreCase) >= 0; }; } } catch (Exception ex) { Logs.WriteAbnormalLOG(54 + ex.Message); } } /// <summary> /// ComboBoxExtensions(ComboBox扩展方法) /// </summary> /// <param name="comboBox"></param> public static void EnableSearchFilter(this ComboBox comboBox) { try { comboBox.ApplyTemplate(); var textBox = comboBox.Template.FindName("PART_EditableTextBox", comboBox) as TextBox; if (textBox != null) { textBox.TextChanged += (s, e) => { var currentText = textBox.Text; var view = CollectionViewSource.GetDefaultView(comboBox.ItemsSource); view.Filter = item => { string itemStr = item?.ToString() ?? ""; return itemStr.Contains(currentText, StringComparison.OrdinalIgnoreCase); }; }; } } catch (Exception ex) { Logs.WriteAbnormalLOG(84+ex.Message); } } }添加防抖
03-27
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值