<local:LevelHostControl x:Name="levelHostControl" Height="580" Width="810" Canvas.Left="0" Canvas.Top="0" HostFile="Level1.xaml"/>
<UserControl 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" x:Class="BeeHive.LevelHostControl" d:DesignWidth="640" d:DesignHeight="480"> <Grid x:Name="LayoutRoot"> <ContentPresenter x:Name="PART_CONTENT"/> </Grid> </UserControl>
using System; using System.IO; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Ink; using System.Windows.Input; using System.Windows.Markup; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Resources; using System.Windows.Shapes; namespace BeeHive { public partial class LevelHostControl : UserControl { public static readonly DependencyProperty HostFileProperty =
DependencyProperty.Register("HostFile",
typeof(object),
typeof(LevelHostControl),
new PropertyMetadata(null, new PropertyChangedCallback(OnHostFileChanged))); public string HostFile { get { return (string)this.GetValue(HostFileProperty); } set { this.SetValue(HostFileProperty, value); } } private static void OnHostFileChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) { LevelHostControl hostControl = (LevelHostControl)sender; string newValue = (string)e.NewValue; if (!string.IsNullOrEmpty(newValue)) { Uri uri = new Uri("/BeeHive;component/" + newValue, UriKind.Relative); StreamResourceInfo streamResourceInfo = Application.GetResourceStream(uri); StreamReader sr = new StreamReader(streamResourceInfo.Stream); object loadedLevel = XamlReader.Load(sr.ReadToEnd()); hostControl.PART_CONTENT.Content = loadedLevel; } } public LevelHostControl() { // 为初始化变量所必需 InitializeComponent(); } } }
原文:http://www.cnblogs.com/gaowen/p/4842488.html