Setting HubSection ContentTemplate programmatically - wpf

I'm developing an application for Windows 8 and am using Hubs and HubSections.
What I'm trying to do is create multiple HubSections depending on the JSON I fetch.
The problem I have is, when I try to set the new HubSection ContentTemplate the program crashes pointing to global::System.Diagnostics.Debugger.Break();
HubSection hs = new HubSection();
hs.ContentTemplate = this.Resources["canteenSectionDataTemplate"] as DataTemplate;
The thing is, if I set hs.ContentTemplate to the ContentTemplate of an already existing HubSection, it works fine, so I'm thinking the problem has to do with the template not being loaded yet?

This won't fix your problem, but it's worth noting that doing this is fairly bad practice for a number of reasons:
HubSection hs = new HubSection();
hs.ContentTemplate = this.Resources["canteenSectionDataTemplate"] as DataTemplate;
Instead of that, when using the as keyword, you should always check for null (unless you know without doubt that it will never be null, in which case you can just cast the value):
HubSection hs = new HubSection();
DataTemplate canteenSectionDataTemplate = this.Resources["canteenSectionDataTemplate"]
as DataTemplate;
if (canteenSectionDataTemplate != null)
hs.ContentTemplate = canteenSectionDataTemplate;
Apart from handling null errors, this code also enables you to check whether the canteenSectionDataTemplate DataTemplate is null or not.
So regarding your question, does the canteenSectionDataTemplate DataTemplate equal null or not? If it does, where is this code being called from? You may need to defer it to make it work.

Related

Does ViewModelViewHost work at design time for WPF? (ReactiveUI)

I cannot get a ViewModelViewHost to work at design time in Visual Studio. Is this by design or have I set something up wrong?
In my view's ctor I have:
Locator.CurrentMutable.InitializeSplat();
Locator.CurrentMutable.InitializeReactiveUI();
Locator.CurrentMutable.Register(() => new SessionView(), typeof(IViewFor<SessionViewModel>));
In my view's XAML I have:
<d:DesignProperties.DataContext>
<local:MainWindowViewModel>
<local:MainWindowViewModel.ChildViewModel>
<local:SessionViewModel/>
</local:MainWindowViewModel.ChildViewModel>
</local:MainWindowViewModel>
</d:DesignProperties.DataContext>
<reactiveUi:ViewModelViewHost ViewModel="{Binding ChildViewModel}"/>
SessionView is an IViewFor<SessionViewModel>.
There is a comment here in the ViewModelViewHost ctor indicating that in design mode it will return before trying to create a view. But it seems strange that InUnitTestRunner should return true if there is a seperate InDesignMode property for that purpose.
// NB: InUnitTestRunner also returns true in Design Mode
if (ModeDetector.InUnitTestRunner()) {
ViewContractObservable = Observable.Never<string>();
return;
}
Bit of a late reply our apologies.
The answer is this is deliberate.
We have stuff that will break the XAML designer if ReactiveUI's code is allowed to initialize in the constructor. Hence why we don't run the values.

Using FindPagesWithCriteria in Episerver returns no results

i wonder if someone else has come accross this issue. I am trying to use FindPagesWithCriteria and i am creating my property criteria as like this:
PropertyCriteria dateCriteria = new PropertyCriteria();
dateCriteria.Condition = CompareCondition.GreaterThan;
dateCriteria.Name = "PageStopPublish";
dateCriteria.Type = PropertyDataType.Date;
dateCriteria.Value = DateTime.Now.ToString();
dateCriteria.Required = true;
so i am trying to find all the pages that are not expired. However, some pages may not have StopPublish property set in which case Datetime.MaxValue should be used. But in this particular case (no StopPublish value set) FindPagesWithCriteria will not return any results. Is there a reason for this or is it a bug? As a workaround, i am returning using PageTypeName criteria and then applying some additional filterin for returned PageDataCollection
FindPagesWithCriteria will only give you published pages (and pages that the current user have access to) so having StopPublish as a criteria is not necessary.
FindAllPagesWithCriteria will return all pages, including unpublished pages and pages the current user does not have access to.
EPiServer properties with an empty value are never stored in the database. If you access it from code, it will always be null.
To search for a null property using PropertyCriteria, set the IsNull property to true

Cloning a PathGeometry in Silverlight / WPF

I have a simple handler that adds an ellipse to an empty Silverlight canvas
private void UCLoaded(object sender, RoutedEventArgs e)
{
var geometry = MakeElipse(20, 15, new Point(100, 100));
var ellipsePath = new Path
{
Data = geometry,
Fill = new SolidColorBrush(Colors.DarkGray),
StrokeThickness = 4,
Stroke = new SolidColorBrush(Colors.Gray)
};
LayoutRoot.Children.Add(ellipsePath);
//
var duplicateEllipsePath = new Path();
//duplicateEllipsePath.Data = ellipsePath.Data;
duplicateEllipsePath.Data = geometry;
duplicateEllipsePath.Fill = ellipsePath.Fill;
duplicateEllipsePath.StrokeThickness = ellipsePath.StrokeThickness;
duplicateEllipsePath.Stroke = ellipsePath.Stroke;
LayoutRoot.Children.Add(duplicateEllipsePath);
}
The first ellipse, ellipsePath, is fine and renders as expected. But the line duplicateEllipsePath.Data = ellipsePath.Data or the alternative duplicateEllipsePath.Data = geometry each throw the System.ArgumentException "Value does not fall within the expected range". How can it be in range once, and out-of-range immediately afterwards? What is the correct way of duplicating a path in code like this?
It looks like the only way to clone a path is to do so manually. To quote this answer from Yi-Lun Luo:
The Data property is actually a Geometry. While not noticeable in Silverlight, A Geometry actually relies on an underlying system resource (because it needs to draw something). If you need to draw another Geometry, you'll need another system resource. So you must clone it before you assign it to a new Path. In WPF, we do have a Clone method on Geometry, unfortunately this is not supported in Silverlight. So you have to manually do the clone.
Another post above Yi-Lun's claims to contain reflective code to clone a geometry, and the same code seems to appear here, although the latter is more clearly formatted. However, in your case, it seems overkill to use a method such as this. The geometry you use is created by your MakeElipse [sic] method. Extracting the common code to generate the geometries into a method seems about the best way to proceed here.
The error message 'Value does not fall within the expected range' is a bit misleading. I don't see anything 'out of range', given that the exact same object was supposedly in range for your first ellipse. I can't say exactly why this error message is reported, but I can speculate. Silverlight is implemented in native code, and I believe that because the native code can't throw exceptions it instead returns numeric error codes. Perhaps there's a limited number of error codes and the one for 'Value does not fall within the expected range' was the one chosen for this error?

ADF Invoke operation manually from code

I want to execute a data control operation (CreateInsert and Delete) from a buttons ActionListener. I am aware a data control button can be inserted from the Data Controls menu, but for various reasons I need to do it this way, a prominent one being I need to perform extra runtime checks.
I found the following code:
OperationBinding operation = bindings.getOperationBinding("operation_name");
operation.getParamsMap().put("parameter_name", parameterValue);
operation.execute();
But don't know which variables to use for myself. First of all, I don't know which binding I should use. Then, the operation name should, as far as I know, be CreateInsert, and for the next button, CreateInsert1. Thats whats used for UIBinding now (which I will remove).
The Data control I want to use the operation of is 'ARNG1'.
So in short, I need to know how to manually invoke this Data control's CreateInsert operation.
Thanks in advance.
See if this will help you:
https://blogs.oracle.com/shay/entry/doing_two_declarative_operatio
the code you want to execute an operation behind a actionlistener:
public BindingContainer getBindings() {
if (this.bindings == null) {
FacesContext fc = FacesContext.getCurrentInstance();
this.bindings = (BindingContainer)fc.getApplication().
evaluateExpressionGet(fc, "#{bindings}", BindingContainer.class);
}
return this.bindings;
}
BindingContainer bindings = getBindings();
OperationBinding operationBinding =
bindings.getOperationBinding("doQueryResultReset");
operationBinding.execute();
Similar to Joe's answer but does not use EL Expression evaluator and uses direct access instead to get the BindingContainer
//Get binding container
BindingContainer bindings = BindingContext.getCurrent().getCurrentBindingsEntry();
// get an Action or MethodAction
OperationBinding method = bindings.getOperationBinding("methodAction");
method.execute();
List errors = method.getErrors();

Silverlight replacement for BinaryReader.ReadDecimal

In Silverlight 4, BinaryReader doesn't seem to have any ReadDecimal() method.
Reflector shows that it's there but with internal visibility, rather than public.
Aside from using that one via dynamic trickery or Reflection, has anyone got a good workaround for getting it. Or is this all part of the plan?
Erica Aside: amusingly, Reflector also shows that there are 10 InternalsVisibleToAttributes in the Ag mscorlib (sadly none to mine :D), which I assume, at 512+ bytes a go gives plenty scope for optimization! I'm sure Bob is in there too :D
There is no direct replacement, but you can achieve the same result like this:
// write it, assume bw = BinaryWriter
var bits = decimal.GetBits(myDecimal);
bw.Write(bits[0]);
bw.Write(bits[1]);
bw.Write(bits[2]);
bw.Write(bits[3]);
// read it, assume br = BinaryReader
var bits = new int[4];
bits[0] = br.ReadInt32();
bits[1] = br.ReadInt32();
bits[2] = br.ReadInt32();
bits[3] = br.ReadInt32();
return new decimal(bits);

Resources