Windows Store Universal App - Dependency Properties - c#

actually i am developing a universal app for Windows and Windows Phone.
Within that app i am using dependency properties to assign data to properties of custom user controls in XAML. The problem is that the compiler says he is not able to find the property were i would like to bind data to.
The button definition looks like the following.
public sealed partial class RulerButton : UserControl
{
public static readonly DependencyProperty BackendProperty =
DependencyProperty.Register("Backend", typeof(GlobalData),
typeof(RulerButton), new PropertyMetadata(null));
public GlobalData Backend
{
get { return (GlobalData)GetValue(BackendProperty); }
set { SetValue(BackendProperty, value); }
}
public RulerButton()
{
this.InitializeComponent();
}
}
I use this button within my main page like this.
<btn:RulerButton Backend="{Binding ElementName=root, Path=BackendSource}"/>
Then the compiler says Unknown Member "Backend" in Element "RulerButton".
If have used the same approach earlier in other projects (Normal WPF-Windows applications) and it worked well. So i think the problem is related to universal app projects.
All other questions i have found could not solve my problem.
I would be appreciated if someone could give me a hint.
greets
Edit
Well i actually have solved the problem. But i think it is very curious. Beside the RulerButton i have many other controls where i added the same dependency properties. On some of them i did not add the normal property Backend yet. Cause i first wanted to make the RulerButton work. After completing these controls so that each of them has a BackendProperty and Backend attribute, all errors were gone.
anyway thanks for your help guys

Well i actually have solved the problem. But i think it is very curious. Beside the RulerButton i have many other controls where i added the same dependency properties. On some of them i did not add the normal property Backend yet. Cause i first wanted to make the RulerButton work. After completing these controls so that each of them has a BackendProperty and Backend attribute, all errors were gone.

Related

How do I apply the Settings.settings values at design time?

I have a panel in my Form like this:
and a Panel_BackColor in project's Settings.setting file:
I can change panel back color in the Form constructor:
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
panel1.BackColor = UI_Settings.Default.Panel_BackColor;
}
}
All things work at runtime:
But nothing change at Design Time. How can I apply these settings at Design mode too?
I got your question, I try to handle with it when I use MetroFramework. Some changes just shown in runtime because it use another drawing technic with xml or .netframework when you use runtime code. So, I think you can't see changes in design time.
Next time try to explain a little more in details or maybe add some code or images to make us understand better.
In C# you have the property:
//Namespace: System.ComponentModel
//Gets a value that indicates whether the Component is currently in design mode.
//true if the Component is in design mode; otherwise, false.
protected bool DesignMode { get; }
I asked "not edited version" of this question on MSDN forum and got an answer within an hour.
Questions like Convert int to string? is a good questions but mine is not!
I think stackoverflow should keep a watch on it's editors and policy.
The answer:
Select your control in Form Designer (for example a button), go to Properties, (ApplicationSettings),(PropertyBinding), then bind BackColor or other property to Button_BackColor or other settings. Afterward by changing settings in Settings.settings file, all binded controls would be affected.

Passing NavigationService to Usercontrol windows phone 7

I want to pass navigationservice to my user control.
At my application side
<myControl:Test NavigationService="{Binding NavigationService}"/>/>
at my Usercontrol CS side
public NavigationService NavigationService { get; set; }
but it dosent seems to work,application crashes as soon as it loads.
That's really harsh... Alright, You haven't mentioned this, but I suppose, that you try to solve the problem of navigating from UserControl to other page/uc(is it true?).
A couple of edits:
First, You don't need an object of class to use a
NavigationSevice.
Second, the NavigationService is most usually used with pages.
If you really do want to use it with UserControl, you should transfer an instance of page to control. But, in my experience, usually, this is not required. I would rather recommend to do in this way:
In your UserControl you will most definitely have the controls with tap/click events. No need to describe their logic in user control class. When you add the user control to page, you should declare it there. Like:
someUserControl.buttonNavigate.MouseLeftButtonUp += new MouseButtonEventHandler(buttonNavigate_Click);
And then you can use navigation service.
NavigationService.Navigate(new Uri(...));
That's the easiest solution. Hope it helps.
There are another ones, but I hope this one will solve the problem.

Refresh View databindings using UpdateLayout or some other alternative

I have an interface IScreenViewModel, and to simplify the problem I can
RedScreenViewModel : IScreenViewModel
GreenScreenViewModel : IScreenViewModel
etc..
This means I have a RedScreenView.xaml which makes a RedScreenViewModel instance, likewise for all subsequent color screens.
IScreenViewModel has some properties that you must implement e.g.
interface IScreenViewModel
{
public Color ScreenColor{get;set;}
}
I have a ViewmodelWrapper class which holds all viewmodels instances. ScreenViewModels, MenuViewModels etc...
Because I am using DevExpress I can't bind the DataContext directly in the Main.xaml.cs file for reasons I still don't know yet.
So in main for example.
I can't have
ScreenLabel.DataContext = viewModelWrapper.ScreenViewModel
I would have to do in main:
DataContext = viewModelWrapper;
This way the parent Window can see all child elements.
In a RedScreenView.xaml I can have something like this:
<Label Background="ScreenViewModel.ScreenColor"/>
And hopefully the data binding should look in the ViewModelWrapper find the IScreenViewModel.ScreenViewModel object and use the correct ScreenColor object using dynamic binding/polymorphism.
There are cases where a screen can have more properties, so let say in
GreenScreenViewModel along with ScreenColor property inherited, it can have its own property maybe DifferentProperty.
The problem is: I have a Factory that returns a screen object depending on what screen the user wants. It returns the correct screen object, but when it notifies the View to update itself it looks at the new object but using the wrong XAML. If that makes any sense.
I do something like this in a ViewModelWrapper method.
MainGui.ScreenWrapper.LayoutRoot.Clear() ;
MainGui.ScreenWrapper.Items.Clear() ;
MainGui.ScreenWrapper.LayoutRoot.Add(screenFactory.GetSelectedScreen("RedScreen").GetLayoutRoot()
MainGui.UpdateLayout() ;
ScreenViewModel = screenFactory.GetSelectedScreen("RedScreen").GetViewModel() ;
Ignore the fact that I called factory twice...
ScreenWrapper is LayoutGroup that holds the screens. When I swap the Views (screens) using that code I am hoping that it would use the correct bindings.
So let's say I swap from GreenScreenViewModel to RedScreenViewModel, remember GreenScreenViewModel one more property than RedScreenViewModel and in the GreenScreenView I had something like this:
<Label Content="ScreenViewModel.DifferentProperty"/>
When the swap is done and ScreenViewModel notifies that is now pointing to RedScreenViewModel it throws an exception. I am strongly assuming this is because Layout isn't being refreshed and it is still using the wrong view.
The output error in debug mode is
"Cannot find property DifferentProperty in viewModelWrapper.ScreenModel"
Which isn't right because I have already deleted THAT GreenScreenView, I updated the layout, I know there is a LayoutChanged event or something like that, so that could have been raised as well so why is it still seeing the wrong View?
How can I update ScreenWrapper.LayoutRoot to "see" the new View with a different binding code.
Heavens, I hope that was clear.
EDIT: At Michael thanks for replying. Yes there is an actual exception - "NullReferenceException" in the thirdparty dll I am using. And that is because it can't find the property. I am sure I didn't make myself clear but maybe the question should be: When deleting and inserting usercontrols from a visual tree -how can I refresh the visual tree to see new bindings? If I can refresh the visual tree it should solve my problem. UpdateLayout() doesn't work
EDIT:
At Michael thanks for replying. Yes there is an actual exception - "NullReferenceException" in the thirdparty dll I am using. And that is because it can't find the property. It throws the exception when I call OnPropertyChanged, and yeah the handler isn't null!
I am sure I didn't make myself clear but maybe the question should be:
When deleting and inserting usercontrols from a visual tree -how can I refresh the visual tree to see new bindings? If I can refresh the visual tree it should solve my problem.
UpdateLayout() doesn't work.
Firstly, you say
Because I am using DevExpress I can't bind the DataContext directly in the Main.xaml.cs file for reasons I still don't know yet.
Express should not be the problem here. You need something to bind to that will return the appropriate ViewModel. Have a look here for discussion on this subject.
Secondly, you say the error is
Cannot find property DifferentProperty in viewModelWrapper.ScreenModel
This is not necessarily a problem, and does not cause an exception. When you change bindings dynamically, INotifyPropertyChanged events fly off all over the place and there may be a period of 'uncertainty'. I am assuming your ViewModels implement INotifyPropertyChanged.
I think the key is probably looking closer at the exception, if there is one (because "Cannot find property" is a debug message, not an exception). To get some clarity you might want to turn off the binding message as described here. If there is an actual exception, please edit with the details.

Drag and drop the user control won't work in .net windows project

I created a user control and it shows up on the tool box as form components. Then when I try to drag and drop the user control on to a form , I get this visual studio error.
" The specified named connection is either not found in the configuration ,not intended to be used with the entity client provider or not valid."
Why am I getting this error?
But some other user controls I can drag and drop which are under the same project. I don't know what I missed in creating this user control.
Beware that code in the UserControl class runs at design time. The constructor, the OnLoad method and Load event. But also methods like OnPaint(). If this code does anything that depends on the environment being setup properly, that code is liable to throw an exception and cause the designer to change its mind about adding the control to the form. That certainly seems to be the case when you get a "not found in the configuration" error, there is no configuration file yet.
Use the DesignMode properly to skip such code. Like this:
protected override void OnLoad(EventArgs e) {
if (!this.DesignMode) {
// Do stuff...
}
base.OnLoad(e);
}
As Hans says, you might need to use the DesignMode property in the Constructor or OnLoad. Also, make sure any public properties that use the connection have this attribute:
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public string Foo
{
get;
set;
}
That way, the designer won't attempt to set them when you add the control to a form. This is always a good habit to get into anyway for properties that you won't be setting at design time.
this error show if you put the code of loading data from database into the constructor of userControl.
"loading data or initialize entity framework"
so the solution is to move the code of loading data from constructor to a method. you can call it "loadData".
and call this method "loadData" in the constructor of the parent form

Static reference to instance in IE Toolbar

I am having an interesting issue with a COM component written to function as a toolbar in IE. Basically if you open up several tabs at once in IE the individual instances of the COM objects get all twisted around. Bear with me here.
Say I open up five browser tabs all at once by right clicking several different links and opening them in new tabs. Now a function of my toolbar involves selecting text in the web page and then clicking a button to copy that text into the Toolbar. So let's do that in tab 3. We select text and click the button and nothing is there. However, if we select text in tab 2, then go back to tab 3 and click the button we get the text selected in tab 2. So...the toolbar in tab 3 getting stuff from tab 2. Not good.
I have traced this problem back to static references inside our COM object, the toolbar.
[ComVisible(true), Guid("2CC75392-1182-470D-BECC-EFA33E629AB8")]
[CLSCompliant(false)]
public sealed class Toolbar : ADXIEToolbar
{
public static Toolbar Instance;
public Toolbar()
{
Instance = this;
InitializeComponent();
}
...other code...
}
Note only one toolbar instance exists per each IE tab.
This reference doesn't get assigned properly, almost like it isn't thread safe (it isn't) but instead not domain safe or something. It will sometimes reference another instance down the line. Same with other static fields and even thread-safe singletons. I don't get it.
Also note that if I pass a reference to this toolbar (inside InitializeComponent) to a control I have the same issue.
this.publicationDateCb.Toolbar = this;
This reference will sometimes point to a different tab.
If I use a purely subscription based model with absolutely zero static references with the toolbar as the referee then things seem to work fine. This basically means I would have to re-design the program to where no classes interacted with each other directly - they fire events that the toolbar subscribes to, calling methods in other classes. Ouch.
So should I go with that model (which may be ideal but I am pretty far along here) or is there a simple fix I am missing here?
Other notes:
All IE tabs are running in seperate processes.
The BHO/Toolbar is running in the same process as the IE tab.
I am using Add-In-Express for Internet Explorer to handle the IE integration.
The project is written for .NET 3.5; the loader uses .NET 2.0
If you want to share your selected text within all your toolbars you can look at: http://www.add-in-express.com/creating-addins-blog/2009/06/19/internet-explorer-plugin-settings-synchronize/
Problem solved but static references are gone. I did a few things:
First off, I changed the target .NET version to 4.0. Apparently BHOs written in 4.0 work better - I can't find a link to substantiate this claim but I have read it somewhere.
More importantly I did away with static references within the assembly altogether. I got rid of the singletons and instead created a property for each former singleton class in my Toolbar class, which will always be unique. I then passed a reference to the Toolbar whenever a class needed to reference a former singleton.
So...constructors look like this now:
internal class RegistryData
{
public RegistryData(Toolbar toolbar)
{
ToolbarRef = toolbar;
}
...
}
And let's say RegistryData needs to call Messaging.
private void RegistryUpdated(int keyId)
{
ToolbarRef.Messaging.SendMessage(keyId);
}
Huge pain, right? Hours of work. But problem solved. I would not be shocked if this issue were related exclusively to Add-In-Express.

Categories

Resources