WPF Prism Request Navigate activation error - c#

Referring the StockTraderRI, I created a popup region in my shell
infBehaviors:RegionPopupBehaviors.CreatePopupRegionWithName="{x:Static inf:RegionNames.SecondaryRegion}"
In the module I am trying to load the view to the popup
_regionManager.RequestNavigate(RegionNames.SecondaryRegion, new Uri("/OrderDetailsView", UriKind.Relative));
OrderDetailsView is a view within OrderDetailsModule. At this point I am getting the below error
Activation error occurred while trying to get instance of type Object, key "OrderDetailsView"
Stack trace looks like below
at Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstance(Type serviceType, String key) in c:\Projects\CommonServiceLocator\main\Microsoft.Practices.ServiceLocation\ServiceLocatorImplBase.cs:line 53
at Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstance[TService](String key) in c:\Projects\CommonServiceLocator\main\Microsoft.Practices.ServiceLocation\ServiceLocatorImplBase.cs:line 103
at Prism.Regions.RegionNavigationContentLoader.CreateNewRegionItem(String candidateTargetContract)
Any ideas what i might be doing wrong?

You must register your objects for navigation. If you are using Prism 6 you must use Container.RegisterTypeForNavigation<OrderDetailsView>();
If using v5 or less you must use container.RegisterType(typeof(object), typeof(OrderDetailsView), "OrderDetailsView");
EDIT: If using MEF, you must provide the view name in the Export Attribute:
[Export("OrderDetailsView")]
public class OrderDetailsView : UserControl
{ ... }

Related

BotFramework: How to handle multiple entities for a single Intent?

In LUIS I have created some utterances for which Intent is detected and I have set 3 different Entities for it i.e For Example I am trying to build a bot to detect user's issue related to an application. So when User enters Unable to Open Android I have set intent as Find_Issue and Entities as 1.Product 2.Issue 3.Error. But since Unable to Open Android doesn't contain any error code. I am getting only 2 entities Product & Issue. Now I want to get the value for Error if no error it would be stored as none.
Here is the Task Code so far
[LuisIntent("Find_Issue")]
public async Task getIssue(IDialogContext context, LuisResult result)
{
EntityRecommendation getProduct;
EntityRecommendation getIssue;
EntityRecommendation getError;
if(result.TryFindEntity("Product",out getProduct))
{
chatdetails.issuedetails.product = getProduct.Entity;
}
if (result.TryFindEntity("Issue", out getIssue))
{
chatdetails.issuedetails.issue = getIssue.Entity;
}
if (result.TryFindEntity("Error", out getError))
{
chatdetails.issuedetails.error = getError.Entity;
}
}
chatdetails.issuedetails is the class created to store the values of Product,Issue,Error
I am not getting how to proceed further.
When defining an intent in LUIS you can also define it's Action Parameters. Action Parameters consists of a parameter name, an entity type, a prompt and if it's required or not.
Then, when using the latest version of the BotFramework Nuget package, your LuisDialog will automatically detect that a required parameter (as in your scenario with the Error entity) is missing and will automatically fire a prompt for the parameter, using the message defined in the Action Parameter.
For technical details about how this thing around the action parameters works, please check this thread.

WPF Prism User Object

I am new to WPF and MVVM Prism. I have been an ASP.NET developer for more than 5 years and recently switched to a WPF project.
I am currently using Prism 5.0 with Unity. The main purpose of following the pattern is to implement modularity and loose coupling.
My question is this: I would like to make my User Object universal and accessible across all modules.
This is what I've done so far. Upon start up, users are greeted with a login screen (LoginView.xaml) in Login project. LoginViewModel will then validate credentials. Upon successful validation, LoginViewModel will then pass this retrieved object to a static class in Infrastructure project. Since user login is only single / universal instance, I have created a static class under Infrastructure project to hold the user object.
I have tried GenericPrincipal, while it does persist data across views, it's not sophisticated enough to hold data that I need. Hence I went for static class instead.
Does anyone have a better suggestion around it?
Instead of registering your User object in a static class, I suggest you to register the User instance in the Unity container itself.
In your LoginViewModel, you should get an instance of your IUnityContainer class.
public LoginViewModel(IUnityContainer container)
{
Container = container;
}
In your Login method, you register your user object:
private void Login(object obj)
{
...
if (user.Authenticated)
{
Container.RegisterInstance("CurrentUser", user);
}
...
}
To access your object, you use the following code snippet:
Container.Resolve<YourUserClassHere>("CurrentUser");
For more details see:
Persisting user credentials in WPF w/Unity and MVVM

Can't use something like "me" or "this" keyword in wf4 xaml activity?

We have implemented a class ActivityException, where the constructor accepts (amongst others) an argument of type System.Activities.Activity. This argument is used to create an error message that contains Activity.DisplayName and Activity.Id.
This works very fine for WF4 activities written in C#. Here I can use something like:
throw new ActivityException(this, ...)
But now I am implementing a WF4 activity in xaml format (i.e. using workflow editor). If I throw an exception here, I have to use the System.Activities.Statements.Throw activity. I filled the Exception parameter of this activity as follows:
new ActivityException(Me, ...)
But the workflow editor says: "'Me' is valid only within an instance method".
I also tried 'this' instead of 'Me', but this produces an error message, too.
Does anybody have an idea how to solve this problem?
If the form name is MainWindow for example you could do:
MainWindow mw = new MainWindow;
new ActivityException(mw, ...)
Even if such a thing eventually worked, the Me you were trying to access would be the Thrown activity and not any other. Moreover, it seems you're using exceptions to log something (?). That's not how you do such a thing.
Not knowing what you're trying to do I would advice you to take a step back a consider other approach. Two options:
1) Create your own activity, which receives said DisplayName and throws an exception:
public class ThrownActivityException : CodeActivity
{
[RequiredArgument]
public InArgument<string> ActivityDisplayName { get; set; }
public override void Execute(CodeActivityContext context)
{
var displayName = ActivityDisplayName.Get(context);
throw new ActivityException(displayName);
}
}
2) Second option, and probably what you want. Considering you're trying to access runtime information (activity's Id) for logging purposes, check Workflow Tracking and its capabilities.

Nhibernate Session closed exception issue with Asp.net mvc telerik ajax grid

I am getting Nhibernate Session closed exception with asp.net mvc telerik ajax grid. The grid is bound to an entity Sale that has a related entity User. The exception thrown is when trying to access the User entity. I have used the fetchMode to eager load it but I still get the same exception. This exception only occur if I switch between the grid pages multiple times. Anyone encountered this issue?
The code for that data access is as follow:
public IList<Sale> List()
{
var manager = new ManagerFactory().GetSaleManager();
var iCriteria = manager.Session.GetISession().CreateCriteria(typeof(Sale))
.SetFetchMode("AspnetUser3.Agents3", FetchMode.Eager);
return iCriteria.List<Sale>();
}
The code that try to access the related object in the object graph is as follow:
AgentId = sale.AspnetUser3.Agents3[0].Id,
The exception thrown
NHibernate.Exceptions.GenericADOException: could not load an entity: [SalesEntry.Data.Model.AspnetUser#7aaabf99-d77d-4edf-b949-9c4c0f3e85d8][SQL: SELECT aspnetuser0_.[UserId] as column1_12_0_, aspnetuser0_.[UserName] as column2_12_0_, aspnetuser0_.[LoweredUserName] as column3_12_0_, aspnetuser0_.[MobileAlias] as column4_12_0_, aspnetuser0_.[IsAnonymous] as column5_12_0_, aspnetuser0_.[LastActivityDate] as column6_12_0_, aspnetuser0_.[ApplicationId] as column7_12_0_ FROM [dbo].[aspnet_Users] aspnetuser0_ WHERE aspnetuser0_.[UserId]=?] ---> System.ObjectDisposedException: Session is closed!
Object name: 'ISession'.
It's happening because User is being lazy loaded and you're likely seeing a situation where the session is being closed before the User object is loaded. Then when the Grid accesses the User reference you get this exception. Make sure you're following the Unit of Work pattern.
Also you may want to use a Model class to bind to your grid, rather than the entity directly. That way you control what kind of access is done and you avoid having unexpected changes to your entities/database.

WP7 - Using Messenger.Default.Send(pageuri) in ViewModel. Without registering for this message anywhere, still navigation is working. How?

I am using MVVM Light toolkit "Messenger" class-
A Messenger class (and diverse message
types) to be used to communicate
within the application. Recipients
only receive the message types that
they register for. Additionally, a
target type can be specified, in which
case the message will only be
transmitted if the recipient's type
matches the target parameter.
Specifically:
public virtual void Send(TMessage message); where TMessage is string for Uri of a View / Page within application.
The method description indicates:
// Summary:
Sends a message to registered recipients. **The message will reach all recipients
that registered for this message type using one of the Register methods.**
Parameters:
message:
The message to send to registered recipients.
Type parameters:
TMessage:
The type of message that will be sent.
In my ViewModel code, I have something like this:
string viewuri = "/View/Page1.xaml";
Messenger.Default.Send(viewuri );
If I register to receive this message in View and try to navigate using something like:
NavigationService.Navigate(new Uri(viewuri, UriKind.Relative));
I get exception:
A first chance exception of type 'System.InvalidOperationException' occurred in Microsoft.Phone.Controls.Navigation.dll
But, if I DONOT register to receive this message anywhere in code, then navigation work without any problem. Can someone please explain this to me? How is this message containing Uri handled by the system?
Thanks.
Update: Please see my comment below. I think there is some problem in my code, the way I am unregistering the messenger in view (MainPage).
Register in ctor:
public MainPage()
{
InitializeComponent();
Messenger.Default.Register<string>(this, m => MessageHandler(m));
SupportedOrientations = SupportedPageOrientation.Portrait | SupportedPageOrientation.Landscape;
}
Unregister in Messagehandler once message is received within MainPage Class:
private void MessageHandler(string message)
{
NavigationService.Navigate(new Uri(message, UriKind.Relative));
Messenger.Default.Unregister<string>(this, m => MessageHandler(m));
}
Unregister doesn't work because you're using an anonymous method (lambda expression). When you write an anonymous method, that creates a new method, so when you unregister with the same lambda expression, it is actually a different method, so Unregister doesn't find a match and doesn't remove the delegate.
Anyway, in that case you don't need an anonymous method at all, you can directly register MessageHandler as the handler:
Messenger.Default.Register<string>(this, MessageHandler);
...
Messenger.Default.Unregister<string>(this, MessageHandler);

Categories