我的WPF窗口中的第二个窗口为空白。
我尝试在多个位置声明变量,并与声明分开分配new Send()
的值。
我还确保它不只是我的计算机。我将编译后的程序发送给其他人,并且做同样的事情。
private void Button_Click(object sender, RoutedEventArgs e)
{
Send send = new Send();
send.Show();
}
发送类:
public partial class Send : Window
{
public Send()
{
InitializeComponent();
ipIdBox.Text = GenerateIPID(GetIPAddress(), 8000);
}
private string GetIPAddress()
{
var host = Dns.GetHostEntry(Dns.GetHostName());
foreach (var ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
return ip.ToString();
}
}
throw new Exception("No network adapters with an IPv4 address in the system!");
}
private string GenerateIPID(string ip, int port)
{
string[] ipseps = ip.Split('.'); // Split IP into parts
string code = ""; // Initialize the code
int counter = 0; // A counter (because every 4 digits are seperated)
foreach (string ippart in ipseps)
{
int ipparti = Convert.ToInt32(ippart);
string hippart = ipparti.ToString("X2");
code += hippart;
counter++;
if (counter == 1) {
counter = 0;
code += "-";
}
}
code += $"-{port.ToString("X4")}";
return code;
}
}
...这是XAML
<Window x:Class="ScistMain.Send"
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"
xmlns:local="clr-namespace:ScistMain"
mc:Ignorable="d"
Title="Send" Height="264.95" Width="451.774" OverridesDefaultStyle="True" Topmost="True">
<Grid>
<TextBlock x:Name="ipIdBox" Margin="0,124,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="34" TextAlignment="Center" FontSize="24" Text="7F00-0001-8000"/>
<TextBlock Margin="0,108,0,0" TextWrapping="Wrap" Text="Tell the person recieving to type this code:" VerticalAlignment="Top" Width="444" TextAlignment="Center"/>
<TextBlock Margin="0,10,0,0" TextWrapping="Wrap" Text="You are sending:" VerticalAlignment="Top" TextAlignment="Center" FontSize="18" FontWeight="Bold"/>
<Image x:Name="iconImg" Height="32" Margin="206,39,206,0" VerticalAlignment="Top" Width="32" Source="Resources/scist.ico"/>
<TextBlock Margin="0,76,0,0" TextWrapping="Wrap" Text="Placeholder Program" VerticalAlignment="Top" TextAlignment="Center"/>
<ProgressBar x:Name="pBar" HorizontalAlignment="Left" Height="34" Margin="10,124,0,0" VerticalAlignment="Top" Width="424" Visibility="Collapsed"/>
<Button Content="Cancel" HorizontalAlignment="Left" Margin="359,203,0,0" VerticalAlignment="Top" Width="75"/>
</Grid>
</Window>
我希望它显示我在设计器中创建的窗口:
但它显示一个黑色窗口。
答案 0 :(得分:1)
OverridesDefaultStyle="True"
上的 Window
。