[英]Interaction between xaml and xaml.cs files within same namespace
<Window x:Class="logf.Circles"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Circles" Height="426" Width="581" Name="Cir">
<Grid Background="Beige">
<Grid.Resources>
<Style x:Key="myfirst">
<Setter Property="Height" Value="150" />
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="Margin" Value="1"/>
<Setter Property="Width" Value="150" />
</Style>
</Grid.Resources>
<Ellipse Style="{StaticResource myfirst}" Fill="red" Name="ellipse1" Stroke="Black" VerticalAlignment="Top" />
<Ellipse Style="{StaticResource myfirst}" Fill="Orange" Name="ellipse2" Stroke="Black" VerticalAlignment="Top" Margin="1" HorizontalAlignment="Center" />
<Ellipse Style="{StaticResource myfirst}" Fill="green" Name="ellipse3" Stroke="Black" VerticalAlignment="Top" HorizontalAlignment="Right"/>
<Button Content="Replace" Name="button1" Tag="c_replace" ToolTip="To replace color" Margin="100" Click="button1_Click" HorizontalAlignment="Stretch" VerticalAlignment="Center" Padding="20" HorizontalContentAlignment="Center" />
</Grid>
</Window>
以上是我的 circles.xaml 的 xaml 代碼
我是 WPF 的新手,想問一些甚至可能很愚蠢的問題。 我創建了一個 xaml 文件並想在 MainWindow.xaml.cs 文件中使用它。 但是,我收到一個錯誤說
“找不到類型或命名空間名稱‘文件名’(您是否缺少 using 指令或程序集引用?)”。
如何在同一個類中進行.xaml and .xaml.cs
文件之間的交互。 謝謝。
在mainwindow.xaml.cs
您應該為您創建的新xaml
頁面運行InitializeComponent()
。
所以只需運行var MyNewXamlWindow = new MyNewXaml();
MainWindow.xaml.cs
方法應在此處調用初始化代碼,以使頁面可供您使用
public MyNewXaml()
{
InitializeComponent();
}
對於您的情況,用 Circles 替換 MyNewXaml
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.