2017-02-27 57 views
-1

我已經有C#.cs沒有.xaml頁面。現在我想在其中使用xaml風格。我可以創建新的xaml頁面並將其與我編寫的.cs類分配嗎?或者,我可以刪除我的班級,並創建新的xaml空的cs?我使用Visual Studio 15 C#。如何創建xaml如果我已經有cs沒有xaml

+0

難道你不能在XAML的代碼中複製和粘貼c#後創建VS? –

+0

我只是,我有興趣是否有一種方法來添加XAML而不是重新創建類 –

+0

好吧,如果XAML與您使用的類具有相同的x:Class名稱,並且該類標記爲partial,它應該可以工作。我敢肯定,這個類還需要至少繼承DendencyObject,但也許還需要UIElement。我會測試這個理論並讓你知道。 –

回答

0

使x:class的名稱與類名相同。確保它們位於相同的命名空間或引用相同的命名空間。

此代碼可以正常工作,並且XAML/Class是相互獨立創建的。

可能想添加'InitializeComponent();'也是類構造函數,但這是按原樣工作的。

<Page 
x:Class="Class_with_XAML_seperate.TestView" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:local="using:Class_with_XAML_seperate" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
mc:Ignorable="d"> 

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> 

</Grid> 
</Page> 


using Windows.UI.Popups; 

namespace Class_with_XAML_seperate 
{ 
    public partial class TestView 
    { 
     public TestView() 
     { 
      new MessageDialog("Hello World").ShowAsync(); 
     } 
    } 
}