I've seen a lot of other ObjectDataProvider questions where the Parameter name: type, name, whatever, cannot be null. Those questions are all due to actual parameters not being set. As far as I can tell, there is no "pattern" parameter for an ObjectDataProvider. The following markup produces "Value cannot be null. Parameter name: pattern", with the accompanying blue squiggle underline. Occasionally the designer throws an exception and fails to load, however pressing the reload button loads the page. The code and markup compile and run as expected. What is causing this?
<Page.Resources>
...
<ObjectDataProvider ObjectType="{x:Type local:AutoFillBox}" MethodName="RecUpdateOutput" x:Key="odpOutput">
<ObjectDataProvider.MethodParameters>
<sys:String>08:00</sys:String>
<sys:String>12:00</sys:String>
<sys:String>13:00</sys:String>
<sys:String>18:00</sys:String>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
</Page.Resources>
Part of the class, wanted to note this is not a custom control, just a poor naming choice:
public partial class AutoFillBox
{
public AutoFillBox()
{ //default }
public string RecUpdateOutput(string time1, string time2, string time3, string time4)
{
//do stuff
}
}
It's the only ObjectDataProvider on the page, and if I remove the 4th string parameter the error goes away. Additionally, the method it calls does take 4 strings, and returns a string so I can bind it's result to an output textbox. I use a similar ObjectDataProvider on a different page with a similar method and signature, and it also shows the same error. What the heck is going on here?
Visual Studio Ultimate 2013, Windows 7 Professional, targeting .net 4.5
I don't believe it's a VS bug. Try adding the property IsAsynchronous="True" to the ObjectDataProvider and that should eliminate the designer error. The property defaults to False and the designer will try to create the object in the active context. Here's link:
ObjectDataProvider
Worked for me. Hope it works for you.
Related
E2: My current solution is wrap the converter code in try block and return the dummy data instead.
In WPF you can specify that a binding should only occur at Design time. I am looking for an inverse of this functionality. But can't find anything. Does such a thing exist?
The reason for this behaviour is that I use a converter and it depends on values that are not initialized at design time.
I tried setting the FallbackValue property to something, but it is ignored.
So I end up with this:
Thanks
E1:
I have this line of code:
<TextBlock Text="{Binding AccountID, Converter={StaticResource IDToNameConv}, FallbackValue=Test}"/>
AccountID is a valid binding. I defined a converter above, that uses values that are not initilized at design time. Which causes NullReferenceException above. I'd like to see some dummy data that I provided in the FalbackValue property, but that does not work.
I'll try binding in code, but isn't there an easier way?
With just this:
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
and this:
mc:Ignorable="d" you have design time binding only. I would think that an inverse exists. But I didn't find anything.
Your IDToName converter should be modified so it can cope with whichever dependency it expects not being available. You don't say what that is, so I can't be more specific.
When that isn't there then you could make the converter just return "Test".
Or you could make it return null. I think then you could use TargetNullValue to return whatever default you prefer. This would be more obvious for any later maintenance than returning a text value from the converter.
Edit
This bugs me for an almost year. I'll update the answer and add bounty.
I've custom control, which has dependency property
public class Graph : Control
{
public List<Figure> Figures
{
get { return (List<Figure>)GetValue(FiguresProperty); }
set { SetValue(FiguresProperty, value); }
}
public static readonly DependencyProperty FiguresProperty =
DependencyProperty.Register("Figures", typeof(List<Figure>), typeof(Graph),
new PropertyMetadata((d, e) => ((Graph)d).InvalidateVisual()));
...
}
Figure is the base class for all figures:
public abstract class Figure { ... }
public class LineFigure : Figure { ... }
public class XGridFigure : Figure { ... }
public class YGridFigure : Figure { ... }
...
Now look at screenshots below to see the problem: sometimes (after doing a change to xaml in other place) designer goes crazy about it and stop rendering the whole window, throwing exceptions, while code compiles and runs without problem. I can close this xaml (designer) and open it again to make problem go away. But it always reappears.
Question: is there something wrong on my side? Missing attribute? Wrong usage? How can I fix that problem?
Old question
Ugly situation.
I have 2 UserControl. In both hand-made control Graph is used. Graph has property Figures to specify List<Figure>. There are dozens of figures which have Figure as base.
In one UserControl it works fine, in other throws exception
The specified value cannot be assigned to the collection. The following type was expected: "Figure".
And I fail to see a difference what could cause a problem.
Here is problematic one screenshot
And here is working one
Despite of errors project compiles and runs, but if I need to do modification to problematic UserControl, then it's not showing any content (says "Invalid Markup"). Graphs are nearly the same, all 8 errors are shown for to just one UserControl.
What should I do? How to troubleshoot such errors? I exclude (completely) any problem with Graph because it runs without a single problem AND it works without problem for another UserControl. Visual Studio designer problem? Using 2013 Express for Windows Desktop.
Indeed the visual designer does not recognize the inheritance from Figure. One solution is to use IList as the Interface type:
public IList Figures
{
get
{
return (IList)GetValue (FiguresProperty);
}
set
{
SetValue (FiguresProperty, value);
}
}
public static readonly DependencyProperty FiguresProperty =
DependencyProperty.Register ("Figures", typeof (IList), typeof (Graph), new PropertyMetadata (new List<object>()));
That might look like a bit strange (because you give up type safetyness). But have a closer look at the WPF classes. They all do it that way (most likely for good reasons). Or WPF even creates collection classes like PathFigureCollection that implement both IList and IList<PathFigure>.
close the project, restart VS and reopen it. does it still list the errors? visual studio often seems to report "phantom errors", but they usually go away if you close and restart etc.
If the custom control is in the same solution or project, Visual Studio builds it (when it considers it necessary) so it can use the control in the designer.
Sometimes this built/cached version gets out of sync with the code files which causes the Xaml parser/syntax checker to get confused and display those wavy red lines.
I have had success with closing and reopening all designers that use the control but that is pretty annoying to keep on doing. In my experience the most reliable solution is to move the control into a separate solution and project and set a 'proper' reference to the dll.
I had a whole load of these errors in one project.
Eventually I found that the project did not have a reference to System.Xaml.
Adding a reference to System.Xaml removed all of the warnings.
The strange thing is that it didn't cause a runtime problem.
I've started creating a Windows Store App for Windows 8.1 and now I encountered a problem concerning localization.
I would like to display a string resource from a .resw file at design time, but every attempt to do so has failed, even though it works at runtime.
When using the x:Uid attribute, I still have to supply the Text property (i.e. for a TextBlock) and I don't like to write the text twice.
I also tried creating a property for the string on the viewmodel:
public string Title
{
get { return ResourceLoader.GetForCurrentView("Strings").GetString("MainView_Title"); }
}
This is working at runtime, but at design time it is blank.
So the question is, is there a way to display resources from a .resw file in the XAML-designer?
More specifically, does the ResourceManager class allow .resw files to be read at design time?
Thanks for your help,
Lucas
Old Method
So, there are a couple of things you can do.
The first (and simplest, given that you're using x:Uid already) is to just supply the text into the Text field. The x:Uid-related value will overwrite whatever is in there.
<TextBlock Text="MyText" x:Uid="MainView_Title"/>
The second method is to use the property like you already have, and then check to see if the app is in Design Time (through a couple of different methods), then return a constant value if it is and the Resource if it is not.
public string Title
{
if(ViewModelBase.IsInDesignTimeStatic) //Mvvm Light's easy accessor
return "My Text";
return ResourceLoader.GetForCurrentView("Strings").GetString("MainView_Title");
}
Hope this helps and happy coding!
Edit: There appears to be a new way to do this, at least as of Windows 8.1.
New Method
Create a class which references a ResourceLoader (similar to the property described above).
Create an indexed property accessor which accepts a string key and return the value from the ResourceLoader.
public class LocalizedStrings
{
public string this[string key]
{
get
{
return App.ResourceLoader.GetForViewIndependentUse().GetString(key);
}
}
}
In your App.xaml, define a StaticResource of this type.
<Application.Resources>
<ResourceDictionary>
<common:LocalizedStrings x:Key="Localized"/>
</ResourceDictionary>
</Application.Resources>
Now, when you want to access your property with entry key MainView_Title, use this. It's more verbose, but it should translate both in the designer and in the app itself.
<TextBlock Text="{Binding Source={StaticResource Localized}, Path=[MainView_Title]}" />
You can shuffle it around to be a bit more readable if you'd like, such as:
<TextBlock Text="{Binding [MainView_Title], Source={StaticResource Localized}}" />
This is an old thread, but since Nate provided such an elegant solution to the problem for Win8.1 I figured I'd ask here...
After much investigation and experimentation, Nate's solution does not appear to work for UWP apps for Win10 under VS2017 Community. The LocalizedString approach works just fine at runtime, but it appears
App.ResourceLoader.GetForViewIndependentUse().GetString(key);
refuses to return anything except String.Empty during design time. I've done a lot of experimenting and things like
ResourceContext.GetForViewIndependentUse().QualifierValues
Seem to be identical between runtime (working) and design time (not working).
I was wondering if anyone has encountered this and solved it. Nate? :)
In my Windows Phone 8 C#/XAML .NET 4.5 App I'm using databinding from ViewModel, which is working fine.
What I'd like is for a lozalized string from LocalizedResources to be displayed as a content of a button in the following cases:
The value returned by Binding is null
The binding could not be resolved
How could this be achieved?
What I've tried to do is:
(omitted TargetNullValue, since the way to do it is probably going to be the same)
(for presentation purposes, i set the resource to be Applicationtitle)
<Button ... Content="{Binding Something, FallbackValue={Binding Path=LocalizedResources.ApplicationTitle, Source={StaticResource LocalizedStrings}}}" ... />
But what I get is text like System.Windows.Text.Data.Binding...(can't read more since it's out of screen).
Did some googling/"stackoverflowing" and found something with valueconverters for WP7, that got me a bit puzzled.
(And added C# tag because I've got a feeling this is not going to be solved just by adding the right "property" to a tag/value to a "property", although I'd appreciate it to be)
I'm pretty sure you cannot apply binding to the FallbackValue. A very simple workaround is to check for null within your 'Something' property.
private string _something;
public string Something
{
get { return _something ?? AppResources.ApplicationTitle; }
set
{
_something = value;
OnPropertyChanged("Something");
}
}
Perhaps it is something trivial but I am out of ideas...
Originally I wanted to add some features to PasswordBox. Because it is a sealed class, original properties have to be replicated, among them PasswordChar. Looks trivial, but when I started to set PasswordChar in Xaml, I could not get rid of parser exception.
At the end I simply defined a new property
public char MyProperty {get; set; }
and tried to set it in Xaml as follows:
<MyPasswordBox MaxLength="3" Password="xxx" MyProperty="c" />
I am getting an exception with the call stack looking like
at MS.Internal.XcpImports.CheckHResult()
at MS.Internal.XcpImports.ConvertStringToTypedCValue()
at MS.Internal.SilverlightTypeConverter.ConvertFrom()
at MS.Internal.FrameworkCallbacks.ConvertValueToPropertyType()
....
at MS.Internal.FrameworkCallbacks.SetValueToProperty()
at MS.Internal.FrameworkCallbacks.SetPropertyAttribute()
....
at System.Windows.Application.LoadComponent()
....
As far I can read it, the type conversion string -> char fails.
Note that whenever I'll change the type of MyProperty to string (for example), everything works.
Does anybody know how to implement char properties so that they can be set from Xaml?
Working on Windows Phone 7, perhaps that's the problem. (Limited SVL 3)
I can't verify this will work, but you can give it a go. The long form xaml syntax should work ok.
Add the following to your namespace imports
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Then the following should work
<MyPasswordBox MaxLength="3" Password="xxx">
<MyPasswordBox.MyProperty>
<sys:Char>c</sys:Char>
</MyPasswordBox.MyProperty>
</MyPasswordBox>
The other solution is to look into type converters to apply to your property so that it'll convert the string for you. Type Convereters and XAML.