Using a bool across multiple pages in WPF - wpf

I've been attempting to pass a bool value across multiple pages. I've built a program that has a login page and four different pages upon successful login. I have an admin and an employee. My problem is that the admin and employee have to have different views -- that is, when the admin is logged in, certain buttons throughout the various pages are visible to him, whereas when the employee is logged in, they are not.
Thus, the problem: How to pass a bool across multiple pages? The bool sets itself upon a login. I was originally passing around the bool through each page, but stack overflow occurs.
My other ideas were to bind the visibility of the buttons to the outcome of the bool, but I've yet to be successful.
Other idea was to make a class for the bool and reference the getters/setters there each time a page opens.
Any ideas?

I guess you want to share a plain normal session in your application.
Just create a model for it
public class SessionModel
{
public bool IsAdmin { get; private set; }
public SessionModel(bool isAdmin)
{
IsAdmin = isAdmin;
}
}
and make this accessible to your Views via MVVM, then in your view check the values of your ViewModel.

Your user class (model) should have the permission info which you should convert to bool/visibility/xyz things to render UI.

Related

different visibility levels of entity properties based on user role (google endpoints)

Imagine we have a class User:
class User {
String id;
String name;
String email;
String password;
}
For a user with admin role, there is no problem to show all properties.
But for a user without admin role, is better to not show some of the properties.
With the annotation #ApiResourceProperty is possible to control the visibility of these properties.
But I'm asking about a good approach to selectively expose (or not) a property, based for example on the role of the user doing the request.
I can use different endpoints for each of the user roles, but I'm wondering if there is a better approach.

CakePHP navigation bar logic

The navigation bar in my application will contain User information (name, profile pic), and some associated data. Since the navigation bar is going to be present in the whole application, I'm putting my logic inside AppController::beforeRender() in order to pass data to the View (in this case, the Element nav_bar).
Is this the right place to implement the logic to retrieve user information and associated data?.
Which of the followings is the correct way to load User model data.
public function beforeRender() {
$this->loadModel('User'); // this one
$this->User->find(...
ClassRegistry::init('User')->find(... // Or this one?
I would save the user information in a Session, otherwise you will be hitting the database in every single request.
So, what you wanna do is: in your login() function you get the user information and save it in a Session. Then, in your element, you simply echo the content you want from your Session.
In your view, Use the AuthComponent class to access to the user information, for example :
to havethe id of user : $use_id = AuthComponent::user('id');
to have all the informations of user : $user = AuthComponent::user();

Using cached data from child window in parent window without a trip to the database

I'm looking for a design approach, so I don't have any code to share.
I have a WPF rich-client application that presents detail data to the user. When the user clicks "Edit" The entire form goes into edit state. This reveals a couple of "Add" buttons. These "Add" buttons open child views providing the user with tools to create new entities. Think "adding a new item to a customer order you're working with". That's the easy part.
When the user closes these child views, the new entities must be displayed and editable in the parent view for continued detailed editing. Something like "add the new item on the child form, pick the part number, then close the child and add quantity and delivery date on the parent view. I don't have any flexibility in this workflow. I have this working also.
When the user is finished with the parent view and is satisfied with the newly added items and detail edits they can click "Save". That is when all the changes need to go back to the database. If the user clicks cancel, all the changes including entities created on the child views must disappear, and the form returned to it's original state.
It's that last bit that stumps me. I'm almost new to Entity Framework, so I thought I could somehow keep the entire set of changes in memory on the client and commit all the changes to the database at the point of user-Save. I don't know if that's possible, or how I have to impliment my data changes to prevent accidental trips to the database.
To put all the facts on the table, I'm using a unit of work pattern, but I have access to an EF context object, if needed. If I have to work with the context object, I must do so across several views and their associated view-models.
I've looked at a whole lot of stackoverflow Q&A but cannot find, or perhaps recognize, a solution path. I've some ideas about using cloned entities, or perhaps transactions, but do not know if there are others or how to choose between them. These would be new skills and require time spent learning. I am willing to spend time learning a new skill, but not learning and trying three or four.
I'm grateful for any advice you might offer.
On the constructor when read from the DB record the value twice. One private variable for current and one for Old.
private string lNameOld;
private string lName;
public string LName
{
get { return lName; }
set
{
if(lName == value) return;
lName = value;
NotifyPropertyChanged("LName");
}
}
public void save()
{
if (lName != lNameOld)
{
// write to database
// really should process all in a transaction
lNameOld = lName;
}
{
public void cancel()
{
if (lName != lNameOld)
{
Lname = lNameOld; // notice capital L so Notify is called
}
{

How to know if you're Dropping in the same app instance where you made the Drag in WPF?

the title pretty much explains my issue:
I am right now taking care of drag & drop in my app. I can have many instances of my app running at the same time, and I can drag from one instance to the other without trouble.
Now, I would like to know if I'm drag & dropping "internally" (i.e: the drop occurs in the same instance as the drag) or "externally" (the opposite)
I went this far: I need to add to my dragged data a unique ID (something like a PUID) that identifies the app where I'm making the drag. Then I can just compare this id to the one I have locally on the drop and see if it is the same.
I have no problem transferring such info in my drag Data, the issue is more to find this UId.
I have been thinking using the Process.GetCurrentProcess().MainWindowHandle; but I'm not sure if this is a good idea.
What option(s) do I have to make this work?
I would simply create a readonly Guid that gets set when you start your app.
You can put this wherever your main logic lives (MainWindow or ViewModel).
Here is a snippet:
public class MyViewModel
{
private readonly Guid mUID = Guid.NewGuid();
// In case you want a property for it
public string UniqueApplicationID
{
get { return mUID; }
}
public void OnDropHandler(MyViewModel objectBeingDropped)
{
if (objectBeingDropped.UniqueApplicationID == mUID)
return;
// Handle drop normally here
}
}
The D-n-D is much like an UI activity, than an internal one.
I would distinguish two contexts: dropping a file, and dropping some object (e.g. VS designer). In the first context there's no problem at all, because it doesn't matter where you pull the data. In the second case, you should know what object has been chosen. For instance, you have a listbox with many items (e.g. the alphabet chars), once the user D-n-D any of those items, the internal operation is a simple reference to the selected object. By pulling the data from another app, you won't be able to find your object, because the source is different.
In case of structs or strings, you may wrap them with a GUID, as you correctly proposed.

Windows Phone 7: Tombstoning with URIs?

I'm building a wp7 app in Silverlight. All of my app's state is stored in NavigationContext.QueryString. If this could be saved on application deactivation, and that page navigated to on application reactivation, that would take care of my requirements for tombstoning.
However, I'm not quite sure how to do this. I was thinking about saving NavigationContext.QueryString to the State dictionary in App.xaml.cs::Application_Deactivated(), but that code doesn't have access to NavigationContext.QueryString. Is there another way I can do this?
I suppose I could just save the query string to the State dictionary every time I navigate, then restore that when the app is re-activated. Or is there a better approach?
Update: Based on indyfromoz's answer, I'd like to implement the following
OnNavigatedToHandler()
{
// save NavigationContext.QueryString in the State dictionary
}
To reduce redundancy, I thought I'd implement this in a class that inherits from PhoneApplicationPage, then have all the rest of my pages inherit from that class. However, I then get the problem that all the page classes are partial because they are also defined in generated code. I don't want to change the generated code, because rechanging it every time it gets regenerated would be a huge pain.
Is there a better way to do this?
Update 2: Here is what I am hacking together now in the main page of my app (the one that is navigated to on startup):
public partial class MainPivot : PhoneApplicationPage
{
public MainPivot()
{
InitializeComponent();
Loaded += new RoutedEventHandler(MainPivot_Loaded);
PhoneApplicationService.Current.Deactivated += new EventHandler<DeactivatedEventArgs>(App_Deactivated);
MessageBox.Show("launching main pivot (state count: " + PhoneApplicationService.Current.State.Count + ")");
if (PhoneApplicationService.Current.State.Count != 0)
{
Debug.Assert(PhoneApplicationService.Current.State.ContainsKey(QueryStringKey),
"State is initialized, but contains no value for the query string");
string oldQueryString = (string)PhoneApplicationService.Current.State[QueryStringKey];
MessageBox.Show("Old query string: " + oldQueryString);
NavigationService.Navigate(new Uri(oldQueryString));
}
}
public readonly string QueryStringKey = "queryString";
void App_Deactivated(object sender, DeactivatedEventArgs e)
{
PhoneApplicationService.Current.State[QueryStringKey] = NavigationService.Source;
}
// ...
It works (sorta) but it's ugly.
Update 3: Looks like the wp7 OS will automatically reload the correct page in a page-based app. I am in fact using pages, so perhaps there's not that much work I need to do here.
However, it doesn't seem to be working. I launch the app, go to a page, hit "Start", then hit "Back". The screen says "Resuming..." but seems to hang there. Is my code supposed to be responding in some way at this point? Is there a way I can keep the debugger attached even after hitting "Start"?
Transient data is usually stored in the State dictionary provided by the PhoneApplicationService class. The data is stored in the OnNavigatedFrom event of the page and restored from the OnNavigatedTo event of the page. If you stored the parameters from the URI of the page in the state dictionary within the OnNavigatedFrom event that is available in every page of your application, you can implement the logic to read the parameters in the OnNavigatedTo event, thereby taking care of Tombstoning
HTH, indyfromoz

Resources