Prism Navigation Not Working In New Window - c#

Edit: Please see my comment below - the code itself seems to be correct (at least the Prism region code) but navigation in the new window instance is still not working.
To start off with, here is the issue I'm having...
I have a main window with a menu bar that switches views in the main window to take the user to different screens. This is all working great.
So I thought today that I would add a 'first time user' screen to handle all the initial setup for the application. This is a new window that will pop up if certain first time properties haven't been set and welcome the new user, get the initial setup complete, etc. I wanted to have navigation on this new window in a new region (just next and back buttons that take the user through the setup).
I thought this would be easy but after 3 hours of floundering and searching the darkest corners of the web I am still very confused - I also looked at Brian Lagunas' Pluralsight videos on MVVM but nothing I have tried to apply is working to setup navigation on the new window.
The content region for the main window is named "ContentRegion" and the content region for the new window is named "SetupRegion".
All views are registered in the bootstrapper like so:
// All views must be registered.
Container.RegisterTypeForNavigation<Home>( "Home" );
Container.RegisterTypeForNavigation<Index>( "Index" );
Container.RegisterTypeForNavigation<Settings>( "Settings" );
Container.RegisterTypeForNavigation<FirstTimeSetupWelcomeScreen>( "WelcomeScreen" );
Container.RegisterTypeForNavigation<FirstTimeSetupScreen2>( "FirstTimeSetupScreen2" );
Here is how the new window is being instantiated currently, from the main window:
public MainWindowViewModel(IRegionManager _regionManager, EventAggregator _eventAggregator)
{
eventAggregator = _eventAggregator;
regionManager = _regionManager;
NavigateCommand = new DelegateCommand<string>(Navigate);
// Set the default view to the home screen
regionManager.RegisterViewWithRegion("ContentRegion", typeof(FirstTimeSetupWelcomeScreen));
// Check to see if program is in first time run or not
if ((ConfigurationManager.GetSection("SaveLocationsGroup/Locations") as IndexLocationsSection).SaveLocation.Location == "")
{
var firstTimeWindow = new FirstTime();
firstTimeWindow.Show();
// Set the default view to the welcome screen on new window
regionManager.RegisterViewWithRegion("SetupRegion", typeof(FirstTimeSetupWelcomeScreen));
}
}
In the XAML for the new window, the content control is setup like this:
<Grid>
<ContentControl prism:RegionManager.RegionName="SetupRegion" />
</Grid>
I have tested by replacing the code in the Navigate command on the main window and having it to navigate to the new user controls, showing them in the main window and that works.
However, in the new window they are not and I can't seem to figure out why. I have also tested to see if the button in the first/default user control view model is firing correctly and it is - for reference, here is that Navigate command code:
private void Navigate(string uri)
{
// WriteLine command to test the button firing
Console.WriteLine(uri);
regionManager.RequestNavigate("SetupRegion", uri);
}
Lastly I've placed the first view inside the main window and it seems to fire correctly, changing the content in the main window - I just can't get it to work or anything to work on the new window at all no matter how I try it. I'm assuming that there is something that I don't know that has to do with either navigation on new instances of windows (aside from the main window) or having to do with containers and new windows but I haven't been able to figure out what. Thank you guys for all of your assistance as always.

It turn out Brian Lagunas has a course on multiple shells I should hopefully be able to use to accomplish what I need. I will attempt to use it.

Related

WInUI 3.0 Desktop - Crash on exception when try to navigate

I want to change frame, but I get this exception:
Navigation:
Frame rootFrame = new Frame();
rootFrame.Navigate(typeof(ScoreWindow), null, new EntranceNavigationTransitionInfo());
Exception appears on constructor:
public ScoreWindow()
{
this.InitializeComponent();
results = new List<Result>();
playerList = new();
LoadData();
var _resultsView = ConvertToView();
sfDataGrid.ItemsSource = _resultsView;
}
Thanks for answers in advance and happy holidays!
P.S. Thanks to Raymond, I detected this message:
WinUI: Error creating second Desktop Window on the current process. No more than one Desktop Window is allowed per process.
There is another question: how to change current frame to other? I mean, I have login view, user logged in successfully and want to see data/other things.
After a little break, I understood what I did wrong. There is a way how to navigate between pages.
Rule #1: In WinUI, you have only one active window, and it's MainWindow. Always. If you want to change layout, you have to use Frames.
In MainWindow.xaml, you write this:
<Grid>
<Frame x:Name="mainFrame"/>
</Grid>
Element "Frame" give ability to navigate between pages.
Then switch to MainWindow.xaml.cs and in constructor have to be something like this:
public MainWindow()
{
InitializeComponent();
mainFrame.Navigate(typeof(NameOfYourPage));
}
It will immediately activate your page.
Then, if you want to navigate from page to page, write in control of page this:
Frame.Navigate(typeof(NameOfYourPage), null, new EntranceNavigationTransitionInfo());
The third parameter is animation, but I didn't notice changes.
More information you can find here:
Tutorial about navigation
Microsoft documentation

How to best implement a log in page with Caliburn.Micro MVVM

I am new to using caliburn.micro and currently learning MVVM.
I am using the windows template studio to create my UWP app, and it works great! But unfortunately, I am not familiar with MVVM and still learning UWP.
I get how the navigation works etc and how the shellpage is loaded. However, I want to prompt the user to log in upon opening the app (i.e. a login in page will start with no navigation sidebar).
I also want to make sure I'm following best practices...
I have tried substituting the MainViewModel with LoginViewModel which I get works, however, I don't want to create the navigation pane. I get that this is triggered by the "new Lazy(CreateShell)". I'm just not sure if I want to remove this from the activation service and call a method upon login?
Below is the default code supplied by the windows template studio which triggers on activation of the app if I understand correctly.
private ActivationService CreateActivationService()
{
return new ActivationService(_container, typeof(ViewModels.LoginViewModel), new Lazy<UIElement>(CreateShell));
}
private UIElement CreateShell()
{
var shellPage = new Views.ShellPage();
_container.RegisterInstance(typeof(IConnectedAnimationService), nameof(IConnectedAnimationService), new ConnectedAnimationService(shellPage.GetFrame()));
return shellPage;
}
I just need to be pointed in the right direction or lead to a video/tutorial as I'm struggling!!! any help much appreciated.
If you want to show login page,you can remove the ShellPage.It is a navigation view.
in App.xaml.cs
private ActivationService CreateActivationService()
{​
return new ActivationService(this, typeof(LoginPage));​
}​
​
private UIElement CreateShell()​
{​
return new Views.ShellPage();​
}
When login successfully,if you want to show the navigation view,you can set the ShellPage to the content of current window.
Window.Current.Content = new Views.ShellPage();

Is this a way to show a modeless dialog from a dockable pane using WPF?

I'm building a Revit plugin. It consists of a dockable pane that (among other elements) has a button. I want to open a new, separate window when a user clicks this button.
At the moment, i create a new Window, but i don't know if that's the right way to go, because now i see two Revit icons on a task bar. I do not have experience as Revit user, i'm new to Revit development, so i'm not sure if this should be the case (two icons) and as silly as it sounds, i do not have admin rights to install random addins and get a feeling of expected user experience.
I create a Window using the following code:
ParametersMissingValueWindow parametersMissingValueWindow = new ParametersMissingValueWindow();
parametersMissingValueWindow.Show();
Based on the understanding of a dockable pane that i have, i think i do not want to create another dockable pane, but just a simple modeless dialog. I wasn't able to find any examples using WPF. Hence, any information whether this is the way to go or help on how to achieve this is highly appreciated.
The Show method takes an optional parent window argument. Specify the Revit main window as the parent window, and your modeless dialogue will be recognised as belonging to the running Revit process. It is accessible from the MainWindowHandle property.
var MyWindow = new MyWindow();
HwndSource hwndSource = HwndSource.FromHwnd(UIApplication.MainWindowHandle);
Window wnd = hwndSource.RootVisual as Window;
if (wnd != null)
{
MyWindow.Owner = wnd;
//MyWindow.ShowInTaskbar = false;
MyWindow.Show();
}
It's not necessary to assign a value to ShowInTaskbar property, but it actually achieves what i wanted to do from the beginning (have only one program open in taskbar), so i left it as part of the solution, but commentted out.
Big thanks to Jeremy Tammik for pointing out the parent property.
You can use WPF to setup a window to use in revit.
MyWPF menu = new menu();
System.Windows.Window wind = new System.Windows.Window();
wind.ShowDialog(); //--> the window shows up and make stuff for revit
if you need the menu to be a dockable one check this source.
Perhaps is not up to date and you will need to adapt the code to the new api.

Dock Windows Forms (tabbed chat interface)

Edit for those who say to use tab control
I would love to use a tab control; yet i have no idea how to go about linking the tab control up from the main form. I would assume that I would have to do something like this:
Create Form with a blank TabControl on it, no pages created.
Create a CustomuserControl (Add -> user Control), with my controls on it.
When a new chat comes in, create a tab control Item, Tab Control Page, add the Custom Control to the Tab Control Page. Add the tab control handle to the hash table, so that when new messages come in, they can be referenced in the proper control.
But, i am so not sure how to do this. For example, I know that I can create custom events inside of the User Control, so that, for example, if each control has a 'bold' button, i can each page that has that control on it, to actually USE the button.
Yet i also need to register message callbacks, so that I can use a MessageGrabber to send data to it, and tha'ts not assigned inside of the UserControl, that's assigned programatically when a new window comes in; but since I have no controls to reference, i can't assign.
KISS Philosophy
Wouldn't it be easier to just create the form, like i do now, and then just dock that form within a window or something? So that, in essence, it's still creating the form, but it's also a separate window?
Original Question
Okay, so i'm stumped (which isn't that big of a surprise when it comes to complex C# logic lol)! What i'm trying to do is the following:
Goal: Setup tabbed chatting for new chat application.
Completed: Open new window whenever a chat message is received, or a user requests a new chat from the roster. This is working perfectly, and opens only a window when the user doesn't already have the chat open. Nice and happy there.
Problem: I dont want windows. Well, i do want A window, but, i do not want tons of separate windows. For example, our Customer Service team may have about 10 active IM windows going at one time, i do not want them to have to have 10 windows tiled there lol. I'd rather they have a single Private IM window, and all 10 tabs docked within the window.
Logic: This is my logic here, which may be flawed, i do apologize:
OnMessage: Open new chat window if one doesn't already exist; if one exists, open it as a tab within the current chat window.
SendMessage: ^^ ditto ^^
Code Examples:
if (!Util.ChatForms.ContainsKey(msg.From.Bare))
{
RosterNode rn = rosterControl1.GetRosterItem(msg.From);
string nick = msg.From.Bare;
if (rn != null)
nick = rn.Text;
frmChat f = new frmChat(msg.From, xmpp, nick);
f.Show();
f.IncomingMessage(msg);
return;
}
Note on above: The Util. function just keeps tracks of what windows are opened inside of a hashtable, that way, when messages come in, they route to the proper window. That is added with the:
Util.ChatForms.Add(m_Jid.Bare.ToLower(), this);
Command in the frmChat() form.
Library in Use: agsxmpp from: http://www.ag-software.de/agsxmpp-sdk/download/
Problem:
How can i convert this code to open inside of tabs, instead of windows? Can someone please give me some ideas, and help with that. I just can't seem to wrap my head around that concept.
Use TabControl

Multi-Page WPF Application

I am new to desktop application development and have a pretty basic question. I have a WPF form named MainWindow, how should I go about having multiple pages on this, such as "User Management", "Manage Content" etc..
I think I have the following options:
Use multiple forms
Tabs
Group Box?
Any clarification would be great!
Well in my most recent application I started by using a TabControl, that's a safe and rather easy way to go.
Recently switched the tabcontrol with a StackPanel with a series of Expanders inside. I styled the expanders to have them display the header vertically and expand horizontally... somewhat similar to the first xbox dashboard. And it looks and works great! =)
Another alternative would be to use a Page instead of a window... Then you would just have to Navigate to each different page.
EDIT:
Here's an example of a multi-page application... might be close to what you need.
The solution I went with that suited what I was looking for was using WPF Pages but thanks for your answers.
There are many ways to do that, such as creating UserControl and show them in the run time.But using TabControl is fast and safe.
Just Use TabControl and place your pages in tab items .Then hide the header of TabControl by setting the value Visibility="Collapsed" to each TabItem.
The result is as below:
As you see the headers are hide and you can switch to each page you want.
Create
usercontrol(wpf): UserManagement
usercontrol2(wpf) : ManageContent
place control "ContentControl" in the main window
Run the code on click of button:
//Displays usercontrol1
contentControl.content = new UserManagement();
//Displays usercontrol2
contentControl.content = new ManageContent();
Hope this helps you.
I'd like to give you an example of something I have in one of my applications.
The app has two windows: the main window and another one (also derived from Window and equipped with the appropriate buttons and event handlers) that is used as a start dialog. The start dialog is called in the constructor of the main window like this:
public partial class MainWindow : Window
{
startdlg m_dlg;
// ...
public MainWindow()
{
m_dlg = new startdlg();
if ((bool)m_dlg.ShowDialog())
{
// ...
}
else
{
Close();
}
// ...

Categories