How to access a DataTemplate in C# - windows

I get confused with Windows 8, as I get used to access a template that i had defined in page like this :
FrameworkElement element = container as FrameworkElement;
DataTemplate myTemplate = element.FindResource("myTemplate") as DataTemplate;
The problem is that in Windows 8 the FindResource method doesn't seem to exist anymore. I try to access the Resource Dictionnary but without any success.
Do you have any idea, or Should the previous example work?
Thank you

FrameworkElement has a Resources property that you can access through it's indexer like so:
var template = element.Resources["myTemplate"] as DataTemplate;
This property is only populated with the resources defined on that element and is not the full collection of resources in the Application. So whether that template will be found depends on where you've defined in.
You can find the resources defined in App.xaml (as well as it's merged resource dictionaries) like so:
var template = Application.Current.Resources["myTemplate"] as DataTemplate;

Related

Adding a query to GoToAsync URI Navigation in Xamarin Forms Shell causes it to stop working

So for this issue I have a pretty simple stack. Main Menu Screen > List Page > Detail Page. For each page I am getting to it using
Shell.Current.GoToAsync({name of page});
so basically a push and then going back in the stack with
Shell.Current.GoToAsync("../");
This is all working fine until I introduced queries to pass data.
So this works fine
Shell.Current.GoToAsync($"{nameof(Page)}");
But this
Shell.Current.GoToAsync($"{nameof(Page)}?Id={some id here}");
Throws this exception
Relative routing to shell elements is currently not supported.
Try prefixing your uri with ///: ///PageName?Id=3AC71D0B-D8E3-6C18-FFE3-6D41E154F000
Which makes no sense because the navigation clearly works without the query included. Where am I going wrong? Is this bug or is it expected behavior?
Relative routing to shell elements is currently not supported.
Try prefixing your uri with ///: ///PageName?Id=3AC71D0B-D8E3-6C18-FFE3-6D41E154F000
That means you are using Relative routes , however this not supports for passing data before Xamarin Forms 4.7.
If using the version of Xamarin Forms before 4.7 .you need to use Absolute routes to pass data , example as follow :
Shell.Current.GoToAsync($"//animals/elephants/elephantdetails?name={elephantName}");
And to receive data, the class that represents the page being navigated to, or the class for the page's BindingContext, must be decorated with a QueryPropertyAttribute for each query parameter:
[QueryProperty("Name", "name")]
public partial class ElephantDetailPage : ContentPage
{
public string Name
{
set
{
BindingContext = ElephantData.Elephants.FirstOrDefault(m => m.Name == Uri.UnescapeDataString(value));
}
}
...
}
More info can refer here : https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/shell/navigation#pass-data
=============================Update===============================
As Shane's said , from the version of Xamarin Forms above 4.7, Relative routes also supports passing paramaters now .
I had a similar issue and solved it by registering the page into the AppShell.xaml.cs
Routing.RegisterRoute(nameof(MyPage),
typeof(MyPage));

How to set cluster resource "Use Network Name for computer name" checkbox programmatically?

I am programmatically setting up a cluster resource (specifically, a Generic Service), using the Windows MI API (Microsoft.Management.Infrastructure).
I can add the service resource just fine. However, my service requires the "Use Network Name for computer name" checkbox to be checked (this is available in the Cluster Manager UI by looking at the Properties for the resource).
I can't figure out how to set this using the MI API. I have searched MSDN and multiple other resources for this without luck. Does anybody know if this is possible? Scripting with Powershell would be fine as well.
I was able to figure this out, after a lot of trial and error, and the discovery of an API bug along the way.
It turns out cluster resource objects have a property called PrivateProperties, which is basically a property bag. Inside, there's a property called UseNetworkName, which corresponds to the checkbox in the UI (and also, the ServiceName property, which is also required for things to work).
The 'wbemtest' tool was invaluable in finding this out. Once you open the resource instance in it, you have to double-click the PrivateProperties property to bring up a dialog which has a "View Embedded" button, which is then what shows you the properties inside. Somehow I had missed this before.
Now, setting this property was yet another pain. Due to what looks like a bug in the API, retrieving the resource instance with CimSession.GetInstance() does not populate property values. This misled me into thinking I had to add the PrivateProperties property and its inner properties myself, which only resulted in lots of cryptic errors.
I finally stumbled upon this old MSDN post about it, where I realized the property is dynamic and automatically set by WMI. So, in the end, all you have to do is know how to get the property bag using CimSession.QueryInstances(), so you can then set the inner properties like any other property.
This is what the whole thing looks like (I ommitted the code for adding the resource):
using (var session = CimSession.Create("YOUR_CLUSTER", new DComSessionOptions()))
{
// This query finds the newly created resource and fills in the
// private props we'll change. We have to do a manual WQL query
// because CimSession.GetInstance doesn't populate prop values.
var query =
"SELECT PrivateProperties FROM MSCluster_Resource WHERE Id=\"{YOUR-RES-GUID}\"";
// Lookup the resource. For some reason QueryInstances does not like
// the namespace in the regular form - it must be exactly like this
// for the call to work!
var res = session.QueryInstances(#"root/mscluster", "WQL", query).First();
// Add net name dependency so setting UseNetworkName works.
session.InvokeMethod(
res,
"AddDependency",
new CimMethodParametersCollection
{
CimMethodParameter.Create(
"Resource", "YOUR_NET_NAME_HERE", CimFlags.Parameter)
});
// Get private prop bag and set our props.
var privProps =
(CimInstance)res.CimInstanceProperties["PrivateProperties"].Value;
privProps.CimInstanceProperties["ServiceName"].Value = "YOUR_SVC_HERE";
privProps.CimInstanceProperties["UseNetworkName"].Value = 1;
// Persist the changes.
session.ModifyInstance(#"\root\mscluster", res);
}
Note how the quirks in the API make things more complicated than they should be: QueryInstances expects the namespace in a special way, and also, if you don't add the network name dependency first, setting private properties fails silently.
Finally, I also figured out how to set this through PowerShell. You have to use the Set-ClusterParameter command, see this other answer for the full info.

CRM SDK OrganizationServiceContext null navigation properties on entity

Trying to migrate an existing solution away from the deprecated Microsoft.Xrm.Client namespace to just use the generated service context from CrmSvcUtil using CrmSDK 9.0.0.5.
Previously we were using Microsoft.Xrm.Client.CodeGeneration.CodeCustomization to get a lazily loaded context.
I have two copies of the same solution and have been working through some of the API changes.
I have enabled Proxy Types
client.OrganizationServiceProxy.EnableProxyTypes();
Which to my understanding switched it to act in a lazily-loaded manner. However, none of the navigation properties are loading as expected.
The few blog posts that I've found around this shift to CrmServiceClient etc suggest that even without lazy loading I should be able to load the property manually with a call to Entity.LoadProperty() which will either load the property or refresh the data. However, after doing that the navigation property is still null (specifically I'm trying to use a Contact navigation property). When I look through the RelatedEntities collection it is also empty.
I know that the entity has a related contact item as if I use a context generated with Microsoft.Xrm.Client.CodeGeneration.CodeCustomization it returns it and I can also see it in CRM itself using an advanced search.
var connectionUri = ConfigurationManager.ConnectionStrings["Xrm"].ConnectionString;
var client = new CrmServiceClient(connectionUri);
client.OrganizationServiceProxy.EnableProxyTypes();
var context = new XrmServiceContext(client);
var result = context.new_applicationSet.FirstOrDefault(x => x.new_applicantid.Id == CurrentUserId);
//result.Contact is null
context.LoadProperty(result, "Contact");
//result.Contact is still null
//result.RelatedEntities is empty

Sitecore DynamicPlaceholder: where are guids stored for renderings?

I'm using DynamicPlaceholders in an MVC implementation of Sitecore specifically for column names in rows. For instance, I might have 3 50/50 renderings on a page, each with a column-one placeholder and a column-two placeholder. The dynamic placeholder code I'm using is working perfectly for creating new pages. But now I need to migrate existing pages from a different node into my new content tree.
I create a new RenderingDefinition item like this:
RenderingDefinition ren = new RenderingDefinition();
ren.Placeholder = "content/50-50";
ren.Datasource = [Datasource GUID];
ren.ItemID = [Rendering GUID];
device.AddRendering(ren);
Then I need to add some content items to each column of that rendering, and here's where I'm running into a problem. Somehow Sitecore stores a GUID with the above rendering I created. If you inspect the placeholder in PageEditor mode you can see a "referenceId" in the markup that corresponds to this. But I cannot figure out where it's stored so I can pass it to the following code:
RenderingDefinition col = new RenderingDefinition();
col.Placeholder = "content/50-50/column-one_[50/50 rendering placeholder guid]";
col.Datasource = [Datasource GUID];
col.ItemID = [Rendering GUID];
device.AddRendering(col);
It's definitely not a property of the RenderingDefintion, nor is it the rendering ID.
The Guids are not stored separately but as part of the placeholder key which can be referenced by referencing DefaultPlaceholderKey property of rendering.Placeholder
Here is a good post on everything you need to know about dynamic placeholders http://johnnewcombe.net/blog/sitecore-part-3

Prism: Nested Regions

I googled a lot but i didn't find an exact answer.
I'm using Prism-Mef, i have RegionB which is nested inside RegionA
When i declare:
IRegion regionB= regionManager.Regions[RegionNames.RegionB];
i got the exception:
The region manager does not contain the RegionB region.
1-How i can access RegionB to Add view to it, knowing that i will use
it as a scoped region.
2-If i have a third level of nesting: for example RegionC which is
nested inside RegionB, how can use RegionC to add a View
Thanks in advance.
Assuming you created a scoped RegionManager - you need to use the scoped region manager of the view that contains the inner region.
var scopedRegionManager = RegionManager.GetRegionManager(view);
var regionB = scopedRegionManager.Regions[RegionNames.RegionB];

Resources