Silverlight UserControl无法正常工作

时间:2014-03-23 14:31:30

标签: c# silverlight xaml exception

我在Silverlight中创建了一个Basic VideoPlayer UserControl。

我尝试将它拖到我的一个页面上然后崩溃了。

这是异常信息

   at Microsoft.Expression.Platform.InstanceBuilders.InstanceBuilderOperations.InstantiateType(Type type, Boolean supportInternal)
   at Microsoft.Expression.Platform.InstanceBuilders.ClrObjectInstanceBuilder.InstantiateTargetType(IInstanceBuilderContext context, ViewNode viewNode)
   at Microsoft.Expression.Platform.InstanceBuilders.ClrObjectInstanceBuilder.Instantiate(IInstanceBuilderContext context, ViewNode viewNode)
   at Microsoft.Expression.SilverlightPlatform.InstanceBuilders.FrameworkElementInstanceBuilder.Instantiate(IInstanceBuilderContext context, ViewNode viewNode)
   at Microsoft.Expression.SilverlightPlatform.InstanceBuilders.UserControlInstanceBuilder.Instantiate(IInstanceBuilderContext context, ViewNode viewNode)
   at Microsoft.Expression.Platform.InstanceBuilders.ViewNodeManager.CreateInstance(IInstanceBuilder builder, ViewNode viewNode)

这是UserControl:CodeBehind

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;

namespace MyProj.Controls
{
    public partial class VideoPlayer : UserControl
    {
        private string _path_to_video;

        public VideoPlayer()
        {
            InitializeComponent();
        }

        public VideoPlayer(string path)
        {
            InitializeComponent();
            this._path_to_video = path;
        }

        private void playButton_Click(object sender, RoutedEventArgs e)
        {
            if(mediaPlayer.CurrentState == MediaElementState.Playing)
            {
                mediaPlayer.Pause();
            }
            else
            {
                mediaPlayer.Play();
            }
        }

        public bool EnablePlayer
        {
            set
            {
                playButton.IsEnabled = value;
            }
        }
    }
}

以下是XAML

<UserControl x:Class="MyProj.Controls.VideoPlayer"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">

    <Grid x:Name="LayoutRoot" Margin="0,0,-53,-67" HorizontalAlignment="Left" Width="453" Height="367" VerticalAlignment="Top">
        <Grid.Background>
            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                <GradientStop Color="Black" Offset="1"/>
                <GradientStop Color="#FFFF0101"/>
                <GradientStop Color="#FF5B0D0D" Offset="0.64"/>
                <GradientStop Color="#FFB20F0F" Offset="0.27"/>
            </LinearGradientBrush>
        </Grid.Background>

        <MediaElement x:Name="mediaPlayer" Margin="10,10,10,50" AutoPlay="False" CacheMode="BitmapCache"/>
        <Button x:Name="playButton" HorizontalAlignment="Right" Margin="0,332,10,10" VerticalAlignment="Bottom" Width="75" Height="25" Content="Play/Pause" Click="playButton_Click" IsEnabled="False"/>

    </Grid>
</UserControl>

更新

经过一番游戏后,我发现当UserControl在我的实现中没有指定名称x:Name=""时会显示异常。

当我添加名称时,错误就会消失。

0 个答案:

没有答案