8,737
社区成员
发帖
与我相关
我的任务
分享
public event LoadCompletedEventHandler LoadCompleted;
void NavigationService_LoadCompleted(object sender, NavigationEventArgs e)
{
var mw = Window.GetWindow(this) as MainWindow;
}
[/quote]
在页面的构造函数里订阅这个事件试试。[/quote]
要下班了吧,预祝国庆快乐!虽然问题还没有彻底解决,还是很感谢您不厌其烦的帮我解困public event LoadCompletedEventHandler LoadCompleted;
void NavigationService_LoadCompleted(object sender, NavigationEventArgs e)
{
var mw = Window.GetWindow(this) as MainWindow;
}
[/quote]
在页面的构造函数里订阅这个事件试试。[/quote]怎么订阅事件?public event LoadCompletedEventHandler LoadCompleted;
void NavigationService_LoadCompleted(object sender, NavigationEventArgs e)
{
var mw = Window.GetWindow(this) as MainWindow;
}
[/quote]
在页面的构造函数里订阅这个事件试试。public event LoadCompletedEventHandler LoadCompleted;
void NavigationService_LoadCompleted(object sender, NavigationEventArgs e)
{
var mw = Window.GetWindow(this) as MainWindow;
}
public class EventClass
{
public static event EventHandler MainEvent = null;
}
private void allinfo_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
//调用事件
if (EventClass.MainEvent != null)
EventClass.MainEvent (this, new EventArgs());//this可以替换成你需要传的东西
}
//主界面加载的时候注册该事件
EventClass.MainEvent += new EventHandler(EventClass_MainEvent);
void EventClass_MainEvent(object sender, EventArgs e)
{
//里面写主界面逻辑
}
var w=Window.GetWindow(this) as MainWindow;
w.GodDod.XXX=XXX;//这里就可以对你的GodDod做操作了,别的以此类推
<!--动态加载按钮-->
<StackPanel Name="CreatBt" Grid.Column="1" Grid.ColumnSpan="2" Grid.Row="1" Orientation="Horizontal"/>
<!--动态加载页面-->
<Grid Name="GodDod" Grid.Column="1" Grid.ColumnSpan="2" Grid.Row="2" />
主函数后台设计
public MainWindow()
{
InitializeComponent();
Frame frame = new Frame();
GodDod.Children.Add(frame);
frame.Navigate(new Uri("Page1.xaml", UriKind.RelativeOrAbsolute));
}
page前台设计
<Grid>
<ListView Name="allinfo" FontSize="15" SelectionChanged="allinfo_SelectionChanged">
<ListView.View>
<GridView>
<GridViewColumn Header="菜鸟" Width="100"/>
</GridView>
</ListView.View>
</ListView>
</Grid>
page后台设计
private void allinfo_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Button Querybt = new Button();
Querybt.Content = "查询";
Querybt.Click += Querybt_Click;
Querybt.SetResourceReference(StyleProperty, "btnStyle");
CreatBt.Children.Add(Querybt);
}
private void Querybt_Click(object sender, RoutedEventArgs e)//查询
{
Frame frame = new Frame();
GodDod.Children.Clear();
GodDod.Children.Add(frame);
frame.Navigate(new Uri("Page1.xaml", UriKind.RelativeOrAbsolute));
}
问题就是page后台设计中,怎么调用MainWindow中的GodDod(Grid名)跟CreatBt(StackPanel名)
var w=Window.GetWindow(this);
这个对象就是你当前页所在的Window了,转成MainWindow就可以用了。
问题是楼主你这低级错误实在是说不过去啊。你New了一个新的MainWindow,和你现在的MainWindow,没有一毛钱关系,那是同一个类型的2个实例,你改了新实例,原有实例是没有变化的。Button btn= new Button();
btn.Click += btn_Click;
btn.SetResourceReference(StyleProperty, "btnStyle");
grid.Children.Add(btn);