I have a UWP app that opens a projection window (similar to the sample app). Now I want react to the RotationChanged-Event in both windows regardless of which window is active.
Can I somehow share the instance for the RadialController? Or do I have to manage it in one view and manually pass the event to the second one?
Updated Question: How do I use the same ViewModel instance in both windows?
P.s: I think adding RadialController and/or Surface-Dial as tags might make sense.
Using UWP, I think you are supposed to use the MVVM pattern.
If you are using the MVVM pattern, having multiple GUI Elements (Windows included) react to a single ViewModel change would be trivial.
So the real question is: Are you use the MVVM pattern, another pattern or no pattern at all?
If you need to learn it first, I wrote something about it regarding to WPF (UWP is a followup of WPF, with App-related backend stuff thrown in):
https://social.msdn.microsoft.com/Forums/vstudio/en-US/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2/lets-talk-about-mvvm?forum=wpf
Unfortunately it's not as simple as using the same ViewModel as #IInspectable pointed out. That's why I sent a command to the second view on every Dial_RotationChanged. Not as pretty as I would have hoped, but works quite well so far.
The code looks somewhat like this:
private async void Dial_RotationChanged(RadialController sender,
RadialControllerRotationChangedEventArgs args)
{
await ProjectionViewPageControl.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
() =>
{
var thePage = (DetailPage)((Frame)Window.Current.Content).Content;
thePage.ProjectionTest(args.RotationDeltaInDegrees);
});
}
Related
At the moment i'm working with Caliburn Micro. But i got to a problem which i don't know how to solve.
The problem is i want to disable Buttons, but every website has only a solution with propertys. the functions of my buttons for example just start a thread to establish a connection over tcp with Netmq. So i don't know how i'll be able to disable them. Searched a lot through google but didn't find anything helpful.
Example of a button function
public void startPubButton()
{
Thread entryThread = new Thread(startPublisher);
entryThread.IsBackground = true;
entryThread.Start();
}
is there maybe a possibility to enable the Buttons only when the thread runs ?
That is the one premise behind CM wiring up by convention all you have to do is provide a CanstartPubButton Boolean property run a code check to see if you can enable or disable button according to the logic with that guard property. Call with NotifyOfPropertyChange(() => CanstartPubButton); in some fashion to do what you want. The logic with in the property (get only needed) is up to you. one other thing I will drop on you is a thread presently in the GitHub discussions on the repository itself. Might help and it might not
https://github.com/Caliburn-Micro/Caliburn.Micro/issues/422
I am a beginner in c# programming and I am developing windows phone application after reading some tutorials.
My idea is when the user clicks a button in a windows page, some other button in other windows phone page must change color from red to green.
Pardon me if I am too Basic.
This I have defined in a page named "IndexPage.xaml"
<Button x:Name="One_green"
Content="1"
Background="Green"
Click="One_Click"
/>
<Button x:Name="One_red"
Content="1"
Background="Red"
Click="One_Click"
/>
Now I see red color button in my window as green button is hidden in the back.
Now, the following code is from another windows phone page "1.xaml"
<Button Content="GO" Click="Button_Click"/>
Now when the user clicks the "GO" Button I want the button to change to red to green in "IndexPage.xaml". So I tried a code something like this in "1.xaml.cs"
private void Button_Click(object sender, RoutedEventArgs e)
{
One_red.Visibility = Visibility.Collapsed;
One_green.Visibility = Visibility.Visible;
}
But I am not able to access the "One_red" or "One_green" button in the above code. Please shed me directions.
Also I want that code to execute only once. (i.e.) when the IndexPage.xaml loads again I want that button to be green always.
Thank you very much in advance.
Please tell me if some other details are required.
You could define a public or internal static variable inside the "Index.xaml" class specifying what button will show on load until otherwise specified. This variable could be accessed outside the class, and possibly even outside the project depending on the modifier chosen. The constructor of the "Index.xaml" class could have code to reset it to the default to ensure it only happens on the next creation of the page. If you aren't creating a new page everytime, you would have to put the default resetters in a method called when you want to bring it to foreground.
It seems to me that you are trying to learn, rather than having a SPEC to follow and implement.
Because of that, and because you are starting with C# in 2014 (almost 2015),
it will be quite beneficial for you to jump straight to data-binding declarative over imperative, going MVVM (MVVx) over MVC (MVx).
XAML was designed around this pattern. It's the natural way of doing things in XAML, a perfect fit and the perfect platform to learn the pattern.
It requires lots of learning, thinking, and re-learning, but it will open your eyes to modern programming techniques.
That said... there are too many ways of doing what you asked for, and while none are exactly wrong, there are 2 current trends in .Net/C#/MsTech which IMO are NOT a waste of your time:
Functional Reactive Programming and OOP/MVVx (the x is for whatever).
Examples are ReactiveUI, Reactive Extensions, PRISM, Caliburn.Micro and many more. They can be combined, the same way you can combine traditional event-driven/event callbacks with MVVM and/or Reactive Programming. However, I would advise against it.
I'll start with the most documented way.
Look at Data binding for Windows Phone 8. It was the first result when I googled "windows phone 8 xaml data binding," and deals with Colors and controls.
If you follow that example and add a resource to your application, you are done.
Of course, you can still use event => onClick + static class to hold the value in between View instances, but if I was right on the assumption that you are trying to learn, I wouldn't go that route.
Sorry if I drifted. :)
You may not be able to access the button click event because it is private, you may need to make it protected or public, the default access specifier would probably be ok as well.
public void Button_Click(object sender, RoutedEventArgs e)
or default would be:
void Button_Click(object sender, RoutedEventArgs e)
I have a Windows Universal App which involves Login.
A typical scenario with this kind of application is different states. For example whether the user is yet to login for the first time or has the user already logged in before and just reopening the app again. Depending on the state here are the actions
IfLoggedIn - Show HomePage
|| IfNotLoggedIn - Show LoginPage
Now based on the condition(state) we have to show different pages.
My question is similar to this but I wanted to understand how to implement the same in MVVM and what is the right place to put this conditional logic.
Putting it in app.xaml.cs will solve the problem but messes up your app.xaml.cs and violates the MVVM because ViewModel is not handling the interaction logic.
The solution that I tried is
Create an intermediate Page(Intermediate.xaml).
Create corresponding ViewModel(IntermediateViewModel.cs) for the above.
In the constructor of this ViewModel incorporate the conditional statement to decide which page to Navigate.
Let us assume I have my condition in the bool variable IsLoggedIn. Where to put the conditional check?
The code in my IntermediateViewModel is as follows
public bool IsLoggedIn {get; set;}
//IsLoggedIn contains the condition of whether the user is logged-in or not-logged-in
public IntermediateViewModel()
{
if (IsLoggedIn == false)
{
NavigationService.Navigate("LoginPage");
}
else if (IsLoggedIn == true)
{
NavigationService.Navigate("HomePage");
}
}
The problem with this solution is now you have one more page(Intermediate.xaml) in your backstack. Is there a way to get rid of this?
This and similar solutions don't appear to be relevant to Windows Universal Apps(Windows 8.1/Windows Phone 8.1) anymore.
Does anyone know how to implement this in the right way with MVVM?
Well, in this case MVVM doesn't hold because this should happen before a view is selected. Why don't you create a class ('Navigator' or whatever) containing your logic, and providing a property ('InitialPage') that you use in the App (instead of the hard-coded initial page) to navigate to the first page ? This way you keep your logic outside of the App class, and there is little modification to do to it.
Please forgive me if this should be phrased differently in a programming context - I have no idea what to search for other than exactly what I used to title my question.
Context - I am attempting to build a word search game involving a 4x4 grid of random letters. I'm currently using a Label_Click event to change the background color of the label containing a given letter. In the future there will be many other actions than just this (evaluating whether an array of clicks forms a legitimate word, for instance), but I'm fundamentally stuck with this problem:
// pseudo-code
if (lblA1_wasClicked || lblB1_wasClicked || ... lblD4_wasClicked)
{
whichever_wasClicked... //perform actions
}
Perhaps I need to learn about creating an event handler for this situation? I have never created a custom event handler, I just know what they are in theory. I am kind of stuck and at this point it's tough to know what to search for to keep learning. I know only enough to be dangerous, so please go easy on me if the answer is as straight-forward as my question feels. :)
If you are in the ui, you can go to the properties of each label and set their Click handler to the same method. so if you have a method with a signature like this.
public void label_Click(object sender, EventArgs e)
{
}
Then assign that to each label and then use that to process each one. This is a naive example, and there are much better methods that use established patterns for creating windows forms applications. Check out this question or search for MVP pattern windows forms to get more info on it!
I'm experimenting with MVVMLight and Windows Phone 7, and so far find it relatively easy. The one thing I can't get my head around is spawning new child windows/views. For example: if I want to create/navigate to a new view to allow a user to edit an item, then refresh the list of items from the database when they return, should I add some sort of handler for every activation of the view, or can I navigate to the edit view, then trigger a callback when the view is closed (NavigationService.GoBack is called).
What I use for this is the Messaging framework. Have the MainViewModel subscribe to a message that should cause it to refresh, then issue that message from the child page. The MainViewModel, still in memory, will hear that message and be able to respond. I have a sample of this on my blog at http://chriskoenig.net/2010/07/05/mvvm-light-messaging/, but note that you can also create your own custom messages (I personally do this all the time) and then just raise them manually:
// In MainViewModel
Messenger.Default.Register<ChildProcessCompleteMessage>(this, () => RefreshData());
// In ChildViewModel
Messenger.Default.Send<ChildProcessCompleteMessage>(new ChildProcessCompleteMessage());
This is a pattern I use quite a bit in my apps to allow communication between the view models. I'm not sure if this fully answers your question, so let me know if you need more info.