I have writen a small applet in Silverlight and, while it works fine on Windows, it seems that on OSX the data binding part of the application (all those NotifyPropertyChanged calls) do not work. Does anyone know why this is? I've tried under both Firefox and Safari with the latest 2.0 download installed.
Your usage of the model object instance in Page seemed odd to me right away. It is not downright incorrect but unusual to me. Some experimentation led me to a working solution, albeit without knowing the cause of the error that happened in the first place. Not many people instantiate objects directly in the DataContext assignment, which is probably why this is not a well-known (and fixed!) defect.
Remove the DependencyObject base class from MyModel.
Make the MyModel instance be a resource of Page, instead of instantiating it directly into the DataContext.
Modify the Button_Click event handler to load the resource, instead of the named Page child object.
All done!
Code snippets for the working solution follow.
Page.xaml
<UserControl.Resources>
<my:MyModel x:Key="TheModel"/>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="White" DataContext="{StaticResource TheModel}">
Page.xaml.cs
private void Button_Click(object sender, RoutedEventArgs e)
{
((MyModel)Resources["TheModel"]).BeginUpdateBitmap();
}
MyModel.cs
public sealed class MyModel : INotifyPropertyChanged
{
Please also include the source code with your question in the future. It would have made this quite a bit simpler.
Did you try using remote sliverlight debugging to the mac? I'd expect getting the debugger setup and turning on 1st chance exceptions has a good shot at showing you the problem.
Related
Sorry. I think I've found it.
I thought the suggestion reference to "RootGrid" was to a VS Named Space, like System.Windows.RootGrid I'm relatively sure they intended me to refer to a Grid in my XAML.
Realizing this probability was a "DUH" moment. So, just replacing RootGrid with my XAML name "GridBoard" will probably solve the issue. I haven't got the close of the Control to work yet, but he naming issue is at least being found.
To answer the question. Help, is a User control file in ScqWander Program. localUCHelp was the name used to create an instance on the Page.
Quick overview: How do I refer to RootGrid?
I get RootGrid "Does not exist in current context"
I am using VS 2019 and have a C# project, using XAML. I wrote a UserControl and am trying code a Button to Close the control (when clicked work from within the control). I got a suggestion which included the sample below. Google has turned up nothing.
The code is within the namespace
public sealed partial class MainPage : Page
void Page_Loaded(object sender, RoutedEventArgs e)
{
ScqWander.Help localUCHelp = new ScqWander.Help();
localUCHelp.HelpUserControlCloseEvent += new EventHandler(BtnPXClose);
RootGrid.Chilren.Add(localUCHelp);
}
I was simply thinking that "RootGrid" reference to which I could refer (as in Using System.Whateverxxxx) when I should have been referring to the Grid I defined in my XAML. I just did not recognize what was being said in the example.
Working on an older application from my predecessor which is using the first floor modern ui framework.
Pretty much by accident we discovered, that the framework tries to refresh the current page when the user presses F5 key reinitializing the respective ViewModel. However, since there are several things triggered from the constructor, this leads to undefined behavior of the application. Additionally the application uses Oxyplot, which also leads to problems with data-binding between PlotView and PlotModel in the ViewModel.
What I have tried so far:
Looking through the source code of the framework so far I couldn't find, where the refresh is triggered, or how to prevent it.
I tried to assign the F5 key to another function, in this case as a shortcut to another page, like this:
<mui:ModernWindow.InputBindings>
<KeyBinding Command="{Binding Path=OpenManualCommand}" Key="F5 >
</KeyBinding>
</mui:ModernWindow.InputBindings>
I found an issue on the github page of the framework, describing pretty much the problem I have. However, it didn't get any replies and being from 2016 it probably won't.
Using the KeyDown event and marking it as handled like this:
private void UserControl_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.F5)
{
e.Handled = true;
}
}
This actually works, however I would have to do the same operation for each view, since it doesn't work if I do it just from the MainWindow. Doesn't seem like a good solution to me.
Is there another way, which let me disable the function of the F5 key with this framework?
I managed to disable this using input binding and command. Command in ViewModel was calling a method by Relay which just had empty return statement in its body.
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 created a WPF application and it runs perfectly on several computers.
There is one computer that keeps throwing an "Object reference not set to an instance of an object." exception.
I can't install Visual Studio on that computer, but i found the line that causes the problem:
var m_GCSWindow = new GCSWindow();
m_GCSWindow.Show();
everything runs perfectly until the Show().
i can't figure this one out because:
it works on all the other computers.
it's MVVM, no code behind.
what could be the problem?
Edit:
I was finally able to install visual studio on the problematic computer, and found that if i remove a single line from the XAML everything loads ok.
<Image Source="{Binding MapView.MapImage,Converter={StaticResource ImageConverter}}"
Stretch="None" MouseLeftButtonDown="Image_MouseLeftButtonDown"
ContextMenu="{StaticResource MapRightClick}" />
so i thought the problem might be in the converter, and put a break point in it, but the exception occurs before.
Edit 2:
After a little more investigation i found that MouseLeftButtonDown="Image_MouseLeftButtonDown" is what causes the problem.
when i remove that line everything works.
private void Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (ImageClickCommand != null) ImageClickCommand.Execute(e.GetPosition(sender as IInputElement));
}
that's the code, and i put a break point in it but still i never get there.
I even tried moving the event to another control, but i still have the same issue.
Maybe m_GCSWindow is null perhaps you could put in a null check .
Does GCSWindow reside in a DLL? Maybe the DLL is missing? Maybe one of its depdencies is missing.
You could also put a try catch around that call
I've had this issue before. The bug can probably be found in the converter
Try debugging the converter
Make sure it gets called
Make sure it gets the value that you want it to convert
Make sure it doesn't crash when converting
Make sure you get the value you want
Etc etc etc
Also - Can you post the code for ImageConverter?
Yesterday I was refactoring some code in my windows phone project to try and use mvvm. I added binding to the toggleswitched on the page etc. the previous code also had evenhandlers for each toggleswitches checked and unchecked events. anyway I managed to clean it up. But my problem occured when I was trying to get code in my viewmodel to execute when i navigated away from this page. Initially I tried this
protected override void OnNavigatingFrom(System.Windows.Navigation.NavigatingCancelEventArgs e)
{
viewmodel.SaveSettings();
}
after a bit of time debugging. I found this method wasnt being called. This was due to the fact that I was calling it in the code behind of a UserControl. Ive also tried to call the OnLostfocus() method when navigating away from the User control. but this doesnt work either. for the most part the project swaps in and out usercontrol elements in the main xaml.cs. mainly iam not really sure how to go about getting this method to be called when the usercontrol exits without destroying the mvvm structure i have i place now. any help would be greatly appreciated.
Solution:
oKay i figured this out. In my main.xaml is had a user contorls beign swapped in and out depending on menu item press by the user. the usercontorl in the main xaml looked like this
<UserControl x:Name="ActiveUserControl" Height="Auto" Width="480" HorizontalAlignment="Left" VerticalAlignment="Top" Grid.Row="1" Margin="0"/>
and it was swapped in and out using this command in the xaml.cs
ActiveUserControl.Content = _userControls["controlname"];
_usercontrols was just a dictionary of usercontrols. so in the onnavigatedfrom method in the main.xaml i changed the content of the ActiveuserControl to some other usercontrol. so if the home key was pressed on this usercontrol it would firethe unloaded event which fired then in the usercontol code behind this way it saved my settings.