在类中初始化UserControl时出错

时间:2014-01-17 20:32:09

标签: c# xaml windows-phone-8

我有一个类“DataLib.cs”和一个UserControl“MediumTile.xaml”。如果我通过以下代码在DataLib.cs中使用UserControl:

var TestTile = new MediumTile();
TestTile.Measure(new Size(336, 336));
TestTile.Arrange(new Rect(0, 0, 336, 336));

我收到此错误:

“System.Windows.ni.dll中出现”System.Windows.Markup.XamlParseException“类型的异常(第一次机会)。”

带有“MediumTile.g.cs”的这一行的标记:

System.Windows.Application.LoadComponent(this, new System.Uri("/DataLib;component/MediumTile.xaml", System.UriKind.Relative));

看起来像InitializeComponent()中有问题;并且实际上不存在目录“component”。

MediumTile.xaml:

<UserControl x:Class="DataLib.MediumTile"
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="336" d:DesignWidth="336">

<Grid x:Name="LayoutRoot" Width="336" Height="336" Background="#FFFF5588">
    <Path x:Name="ico_Cloud" Width="180 " Height="110" Canvas.Left="0" Canvas.Top="0" Stretch="Fill" Fill="#FFFFFFFF" Data="F1 M 67.8062,198.672C 30.3579,198.672 4.57764e-005,168.314 4.57764e-005,130.866C 4.57764e-005,93.4175 30.3579,63.0596 67.8062,63.0596L 71.932,63.0596C 85.0272,26.3081 120.131,0 161.379,0C 202.627,0 237.73,26.3081 250.826,63.0596L 256.307,63.0596C 292.4,63.0596 322.757,93.4175 322.757,130.866C 322.757,168.314 292.399,198.672 254.951,198.672L 67.8062,198.672 Z "/>
    <TextBlock x:Name="testTXT" Text=""/>
</Grid>

MediumTile.xaml.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;

namespace DataLib
{
public partial class MediumTile : UserControl
{
    public MediumTile()
    {
        InitializeComponent();
        TileAktualisieren();
    }

    public async void TileAktualisieren()
    {
        List<String> DatenPacket = await DataLib.Daten();
        string Ort = DatenPacket[0].ToString();
        string Temp = DatenPacket[1].ToString();
        string Code = DatenPacket[2].ToString();

        testTXT.Text = Ort;
    }
}
}

DataLib.cs中的Daten():

public async static Task<List<String>> Daten()
    {
        IsolatedStorageFile DatenDateien = IsolatedStorageFile.GetUserStoreForApplication();

        string Ort = String.Empty;
        string Temp = String.Empty;
        string Code = String.Empty;


            // Http Request
            HttpClient client = new HttpClient();
            string url = "http://api.worldweatheronline.com/free/v1/weather.ashx?q=Berlin&format=json&num_of_days=5&key=xxxxxxxxxxxxxx";
            string DatenURL = await client.GetStringAsync(url);
            RootObject apiData = JsonConvert.DeserializeObject<RootObject>(DatenURL);

            // Write Data into isoStorage
            using (Mutex mutex = new Mutex())
            {
                mutex.WaitOne();
                try
                {                                        
                    IsolatedStorageFileStream WritingStream4 = new IsolatedStorageFileStream("Ort.txt", FileMode.Create, DatenDateien);
                    StreamWriter writer4 = new StreamWriter(WritingStream4);
                    writer4.Write(apiData.data.request[0].query);
                    writer4.Close();

                    IsolatedStorageFileStream WritingStream5 = new IsolatedStorageFileStream("Temp.txt", FileMode.Create, DatenDateien);
                    StreamWriter writer5 = new StreamWriter(WritingStream5);
                    writer5.Write(apiData.data.current_condition[0].temp_C);
                    writer5.Close();

                    IsolatedStorageFileStream WritingStream6 = new IsolatedStorageFileStream("Code.txt", FileMode.Create, DatenDateien);
                    StreamWriter writer6 = new StreamWriter(WritingStream6);
                    writer6.Write(apiData.data.current_condition[0].weatherCode);
                    writer6.Close();
                }
                finally
                {
                    mutex.ReleaseMutex();
                }              

            }
        }

        // Read Data from isoStorage
        using (Mutex mutex0 = new Mutex())
        {
            mutex0.WaitOne();
            try
            {
                IsolatedStorageFileStream ReadingStream4 = new IsolatedStorageFileStream("Ort.txt", FileMode.Open, DatenDateien);
                StreamReader reader4 = new StreamReader(ReadingStream4);
                Ort = reader4.ReadToEnd();
                reader4.Close();

                IsolatedStorageFileStream ReadingStream5 = new IsolatedStorageFileStream("Temp.txt", FileMode.Open, DatenDateien);
                StreamReader reader5 = new StreamReader(ReadingStream5);
                Temp = reader5.ReadToEnd();
                reader5.Close();

                IsolatedStorageFileStream ReadingStream6 = new IsolatedStorageFileStream("Code.txt", FileMode.Open, DatenDateien);
                StreamReader reader6 = new StreamReader(ReadingStream6);
                Code = reader6.ReadToEnd();
                reader6.Close();

                // here is the part with the UserControl
                var TestTile = new MediumTile();
                TestTile.Measure(new Size(336, 336));
                TestTile.Arrange(new Rect(0, 0, 336, 336));

                using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    if (!store.DirectoryExists("shared/shellcontent"))
                    {
                        store.CreateDirectory("shared/shellcontent");
                    }  
                    var bitmap = new WriteableBitmap(336, 336);
                    bitmap.Render(TestTile, new TranslateTransform());
                    var stream = store.CreateFile("/shared/shellcontent/test.jpg");
                    bitmap.Invalidate();
                    bitmap.SaveJpeg(stream, 366, 336, 0, 100);
                    stream.Close();
                }

                // Tile
                ShellTile PinnedTile = ShellTile.ActiveTiles.First();
                FlipTileData UpdatedTileData = new FlipTileData
                {
                    Title = Temp + "°c",
                    BackTitle = Ort,
                    BackgroundImage = new Uri("isostore:/shared/shellcontent/test.jpg", UriKind.RelativeOrAbsolute),
                };
                PinnedTile.Update(UpdatedTileData);

                }
            }
            finally
            {
                mutex0.ReleaseMutex();
            }
        }

        return new List<String> { Ort, Temp, Code };
    }
}

1 个答案:

答案 0 :(得分:1)

试试这个:

<Grid x:Name="LayoutRoot" Width="336" Height="336" Background="#FFFF5588">
    <Path x:Name="ico_Cloud" Width="180" Height="110" Canvas.Left="0" Canvas.Top="0" Stretch="Fill" Fill="#FFFFFFFF" Data="F1 M 67.8062,198.672C 30.3579,198.672 4.57764e-005,168.314 4.57764e-005,130.866C 4.57764e-005,93.4175 30.3579,63.0596 67.8062,63.0596L 71.932,63.0596C 85.0272,26.3081 120.131,0 161.379,0C 202.627,0 237.73,26.3081 250.826,63.0596L 256.307,63.0596C 292.4,63.0596 322.757,93.4175 322.757,130.866C 322.757,168.314 292.399,198.672 254.951,198.672L 67.8062,198.672 Z "/>
    <TextBlock x:Name="testTXT" Text=""/>
</Grid>

路径宽度为180后,您有space