使用UserControl制作自定义实时磁贴会导致System.IO.FileNotFoundException

时间:2013-11-15 19:06:55

标签: c# user-controls windows-phone

我正在尝试使用在我的后台代理中引用的另一个项目中的UserControl从后台代理创建自定义实时磁贴。

我的用户控件:

<UserControl x:Class="livetile.smalltile"
    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"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    d:DesignHeight="159" d:DesignWidth="159">

    <Canvas x:Name="LayoutRoot" Background="#ea6060" Width="159" Height="159">
        <TextBlock Text="{Binding day}" Width="159" TextAlignment="Center" FontFamily=".\Fonts\Helvetica-Light.otf#Helvetica Light" FontSize="100" Foreground="White"/>
        <TextBlock Text="{Binding dayofweek}" Width="159" TextAlignment="Center" Canvas.Top="100" FontFamily=".\Fonts\Helvetica-Light.otf#Helvetica Light" FontSize="30" Margin="0,0,0,25" Foreground="White"/>
    </Canvas>
</UserControl>

我的代码背后:

public partial class smalltile : UserControl, INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(string p)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(p));
    }

    private string _day;
    public string day
    {
        get { return _day; }
        set
        {
            if (_day == value) return;
            _day = value;
            NotifyPropertyChanged("day");
        }
    }

    private string _dayofweek;
    public string dayofweek
    {
        get { return _dayofweek; }
        set
        {
            if (_dayofweek == value) return;
            _dayofweek = value;
            NotifyPropertyChanged("dayofweek");
        }
    }

    public smalltile()
    {
        InitializeComponent();
        this.DataContext = this;
    }
}

我的后台代理中的代码:

var smalltilevar = new smalltile();
smalltilevar.day = DateTime.Now.Day.ToString();
smalltilevar.dayofweek = DateTime.Now.DayOfWeek.ToString();
smalltilevar.Measure(new Size(159, 159));
smalltilevar.Arrange(new Rect(0, 0, 159, 159));

使用断点时,我看到它在我的UserControl的构造函数中崩溃并出现以下错误:

An exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.ni.dll and wasn't handled before a managed/native boundary
A first chance exception of type 'System.UnauthorizedAccessException' occurred in System.Windows.ni.dll

我真的不知道我做错了什么?

1 个答案:

答案 0 :(得分:0)

好的,我想通了,我必须使用部署调度程序,如下所示:

Deployment.Current.Dispatcher.BeginInvoke(() =>
{
    var smalltilevar = new smalltile();
    smalltilevar.day = DateTime.Now.Day.ToString();
    smalltilevar.dayofweek = DateTime.Now.DayOfWeek.ToString();
    smalltilevar.Measure(new Size(159, 159));
    smalltilevar.Arrange(new Rect(0, 0, 159, 159));
});