How to keep shared data without using singleton in Xamarin.forms - xamarin.forms

I have a ContentPage with a ListView. I'm navigating to this content page using another content page's button. This button shows the list item count. I have ViewModel connected to each content page. I can update the count when add items to my ObservableCollection since field is adding not from the list page. But when I delete an item from the list view, I want to update the count in that time as well. How can I keep this ObservableCollection as a shared one between these 2 ContentPAges? I know using aSingleton class we can do this. But since my code should support for unit testing it seems I cannot do this.
What is the best way to make this ObservableCollection shared among these content pages?

If I understand correctly you need a single data repository to be shared between multiple ViewModels. Which means that you need to keep a single instance of the repository somewhere in your code base. Depends on your project setup this repository can live in the IOC container or as a simple property within the App class or elsewhere. Also, depends on the collection data type you also may need to persist the data to SQL, local storage or etc. This way it will be trivial to access the collection from any ViewModel in the application.
However, since I don't understand your intents fully, there might be other solutions. Like passing the reference of the collection to the next page and etc.

If the only information you are wanting to share is the actual count of the items in the list, you could use the MessagingService that is build in in Xamarin Forms.
https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/messaging-center
So in your example maybe create a class that holds all the info that you want to share and in one page do a
MessagingCenter.Send<ShareInfo>(...)
So you can pass data.
And in the other page do
MessagingCenter.Subscribe<ShareInfo>(...)
To receive the data and use that to update the button count.

Related

How to get assigned Target Audiences from List or WebPart?

I have added my List as a webpart to a web page, and I set a specific 2 target audiences for it (Owner, Customer).
However, now I would like to use C#, JS, REST to get assigned audiences from list or web part.
Please help.
Thanks
This behavior is set on the AuthorizationFilter property of the Web Part itself. You can see an example of that here:
https://sharepoint.stackexchange.com/questions/138566/adding-target-audiences-programmatically
You'll need to use the WebPartManager to get ahold of the WebPart on the page that you care about so that you can inspect it. You'd then use the AudienceManager's helper methods to parse out the AuthorizationFilter's value to turn it into human readable groups and audience names.

Asp.net setting some context for a Custom UITypeEditor from parent form at Design Time

Trying to craft a way for our designers to create custom asp.net webforms for our customers. The basic idea is that we have a general set of fields that can be saved, with each customer using a subset of those fields, each customer may have it's own names for similar concepts (ie one customer may use 'patient' and another 'customer'). Staying in customer context will help the designers build the correct form to meet the customers needs.
Now to do this I need to get as much as I can into the design experience as possible. I have a set of custom controls that derive from base controls like asp:TextBox and implements an interface to allows me to iterate over the controls and insert values from a data store, or fetch values to persist (Done). All of these controls need to be bound to a data point in my record object. I have a metadata system set up to allow the binding, but where I am running into trouble is in getting the list of available fields into a UITypeEditor. Namely, I haven't figured out how to (at design time) set the customer context on the form, and then access and use that value on each controls custom property editor (also at design time). Is this possible? Am I looking at this problem the wrong way? A form always has 1 and only one customer context. So in my forms project, each form will have a different customer id set to it.
As a second question how can I debug these editors? There is an expectation of web context in the editors, but there is no asp.net property grid. and even if there was, the editors are winforms components that wouldn't run in a browser.
I can include the code I currently have if it simplifies the problem, but even a simplified example is a half dozen classes.
I resolved these issues, posting here in case a general solution can be useful to anyone in the future.
I was able to get around this problem a bit by going the other way. My parent Page iterates over it's controls, and sets the CustomerId on all the controls that implement a specific interface.
As for debugging. Some refactoring got me to a place where I can easily swap out the webforms specific parts of my implementation for testing.

Need example of a web form editing a parent entity object with children

I'm working on an "edit" page for an object. On this page, I would like to have something like a DetailsView allowing editing of properties of the object as well as a GridView with children and allowing for simple Add/Edit/Delete of the children. The updates to the parent or child objects shouldn't save to the database until a Save button is clicked. I'm asp.net web forms and Entity Framework
As a simple example, imagine a Project object with a collection of Resources. I am building a page that allows a user to change Project properties such as ProjectName in a DetailsView as well as add/edit/deleting a Resource in a Gridview below. One Save button at the bottom saves everything.
I've found examples of this in MVC, but am having trouble finding a web forms example.
I am not able to code your application for you but I have accomplished this using one of two variants of the same pattern.
Create a record object for your children that is serializable and maintain that in a ViewState based List. If the data is significantly large, consider maintaining it in Session and use a unique key to your Session object (such as a GUID assigned at Page_Load) and stored the key in ViewState.
When your use is done editing the parent / children, you will have to resolve the changed to your ViewState / Session based objects to those stored on disk.

ASP.NET Dynamic Data and uploading files

I am developing a small, internal-use only web application. Given its simple nature and intended audience, I decided that it might be a good opportunity to use a ASP.NET Dynamic Data project to get things up-and-running quickly. So far so good, except for one issue that has me reconsidering the whole plan:
I need to be able to upload files through the website. There is an entity in the model that represents an uploaded file. This entity has properties for the file's contents, the file name, and the file's content type. When uploading a file, all of these values are obtained from a single FileUpload control.
Since a FieldTemplate has a one-to-one association with an entity property, I decided that I needed to create a custom EntityTemplate for the File entity. At this point, I have created an "edit" template for the entity that has a FileUpload control. What I have not been able to figure out is, when the user clicks the 'Update' link, how do I get the data from the FileUpload control back into the entity and (ultimately) into the database.
Any advice or guidance is much appreciated.
you can add other values to the dictionary in the ExtractValues method of the FiedlTemplate, what you have to be aware of is that if those values also appear as rows or column in the page template then they will overwrite the values you added. I usually scaffold them as false and then only referance them in the custom field template.
Note you can access their initial values from the Row property in the OnDataBinding event, you can cast the property as the actual type or use an interface added via buddy classes.

How to make rich/compound views

I have recently started to examine ASP.NET MVC. I have studied quite a few examples and common to these are that they contain quite simple scenarios, where a view will map to either an instance of a type in the model or a list of a paritcular type from the model.
I'm looking for guidelines to compose/composite views. In the long term I would like Ajax to be part of the equation, but for now I'm looking for a simpler non Ajax setup.
The problem
Here is a description of a contrieved problem. A domain model contains the types A and B.
class A
{ String ID, String Name, int Age
List<B> ListOfB
}
class B
{ String ID, String Name, String Url}
I would like to have a that allows the following:
A DropDownList showing type A information
Details about the particular A picked in the dropdown
A list showing type B's related to the selected type A
A form that makes it possible to edit the data of a selected type A
A form that enables the user to add a new B.
The view should not show all of this at once, instead it has to show different combinations of information and functionality according to user input. Combinations of detail and functionality could forexample be:
Initially only show the dropdownlist containing A's
If an A has been selected in the dropdown:
Show the particular A as selected in the dropdown
Show detail info about the selected A
Show list of detail info of related type B's
The user wants to edit a particular A
Show the particular A as selected in the dropdown
Show form that allows user to edit particular A
Show list of detail info of related type B's
The user wants to add a new B
Show the particular A as selected in the dropdown
Show detail info about the selected A
Show list of detail info of related type B's
That could look something like this (used the web version of balsamiq mockups - what a fantastic invention!):
Combination 2:
Combination 4:
Creating the view and controller
Since the solution has to allow for different combinations of data and functionality, I think it would be smart to have a parent view (not to be confused with a masterpage) that contained placeholders for parital views. Then, the parent views controller could make up the right combinations of model data and partial views and feed these to the parent view.
Finally; the questions:
Is my way of thinking in accordance with the asp.net mvc methodology?
Can you explain/show (not necessarily all of it) how the controller can compile the right combination of partial views and feed these to the parent view?
Can you point me towards an Ajax based solution?
Can you suggest books/links that contain examples of complex views?
asp.net mvc fully supports all of your requirements but there are few things you should get up to speed on:
You should look at implementing view models to help seperate your domain model from your specific views. Here is a good link on how to start this.
You need to get up to speed with a client side javascript framework for the ajax work with partial html rendering. jquery will do this or ms ajax. Here is an example
To your detailed questions:
Is my way of thinking in accordance with the asp.net mvc methodology?
Asp.net mvc is not going to constrain you at all so essentially this is fully supported
Can you explain/show (not necessarily all of it) how the controller can compile the right combination of partial views and feed these to the parent view?
You can use partial views if you want to seperate bits of code out and can easily refresh them by loading them independently using ms ajax or jquery. You would have a controller that mapped onto your parent view and can delegate and refresh partial views in ajax calls.
Can you point me towards an Ajax based solution?
jquery will do this or ms ajax. Here is an example
Can you suggest books/links that contain examples of complex views?
This link talks a lot about this.

Resources