我已经获得了代码,其中MainWindow中的字符串变量名为" repeatNumber" - MainWindow中还有两个按钮。 btn2应该打开一个新窗口' otherwindow'其中包含repeatNumber字符串并显示图片。
'窗口1'不包含' getRepeatedNumber'的定义没有扩展方法' getRepeatedNumber'接受类型' Window1'的第一个参数。可以找到。
任何人都可以帮我吗?这可以在不使用MVVM的情况下修复吗?代码如下:
Mainwindow.cs
public partial class MainWindow : Window
{
private string repeatNumber;
public MainWindow()
{
InitializeComponent();
string[] assignments = new string[] { "https://cdn2.iconfinder.com/data/icons/animals/48/Turtle.png", "https://cdn2.iconfinder.com/data/icons/animals/48/Butterfly.png", "https://cdn2.iconfinder.com/data/icons/animals/48/Dolphin.png", "https://cdn2.iconfinder.com/data/icons/animals/48/Elephant.png", "https://cdn2.iconfinder.com/data/icons/animals/48/Hippopotamus.png", "https://cdn2.iconfinder.com/data/icons/animals/48/Panda.png" };
Random rnd = new Random();
string[] randomingArray = assignments.OrderBy(x => rnd.Next()).ToArray();
List<Images> animals = new List<Images>();
for (int i = 1; i < 100; i++)
{
if (i == 9)
{
repeatNumber = randomingArray[i % randomingArray.Length];
animals.Add(new Images() { Source = repeatNumber, Number = i });
}
else if ((i % 9) == 0)
{
animals.Add(new Images() { Source = repeatNumber, Number = i });
}
else
{
animals.Add(new Images() { Source = randomingArray[i % rnd.Next(1, 5)], Number = i });
}
ItemsControl1.ItemsSource = animals;
}
}
private void btn1_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("test");
}
private void btn2_Click(object sender, RoutedEventArgs e)
{
Window1 otherwindow = new Window1();
otherwindow.Show();
string value = otherwindow.getRepeatedNumber; <--- This line gives me the error
}
}
class Images
{
public int Number { get; set; }
public string Source { get; set; }
}
Mainwindow.xaml
<Grid>
<ListBox x:Name="ItemsControl1">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="5">
</UniformGrid>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<Border BorderBrush="Black" BorderThickness="3" Width="Auto" Height="Auto">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Number}"/>
<Image Source="{Binding Source}" Margin="0,0,5,0"/>
</StackPanel>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Grid>
<Button x:Name="btn1" Click="btn1_Click" Height="20" VerticalAlignment="Bottom" Margin="127,0,316,0" Content="Instruction"></Button>
<Button x:Name="btn2" Click="btn2_Click" Height="20" VerticalAlignment="Bottom" Margin="300,0,109,0" Content="Results" Width="74"></Button>
</Grid>
</Grid>
Window1.cs
public class Window2 : Window
{
public string getRepeatedNumber { get; set; }
public Window2(string repeatNumber)
{
getRepeatedNumber = repeatNumber;
}
}
和Window1.xaml只是
<Grid>
</Grid>