Changing Views in Fragments - c#

In My app, i use an altered Example of the ViewPagerIndicator, which launches 5 Fragments. so far all works well, but the fragments do have to change their View (completly new layout) for other purposes. how do i do that? a Fragment doesn't have a function SetContentView like activities do. so is there a way to update the view or something like that?

Use Fragment.getView() to get the root view of the Fragment. On that View, call removeAllViews(). Then build your new views and add them to the view you got from getView().
Edit
I never used Mono for Android before, but looking at the documentation; I suggest you put the inflated View from inflater.Inflate in a private member of your Fragment. Later on, when needed, you can use that reference to edit your layout.

Related

MvvmCross can't add second xaml view for other device family

In a UWP project you can add a new xaml view with the same name without the class background to create a specific view for a platform, for example mobile.
So I have my general view with code behind class:
PaymentListView.xaml and PaymentListView.xaml.cs.
And I wanted to create a new view for Mobile. So I created a XAML view with the name:
PaymentListView.DeviceFamily-Mobile.xaml
Both have the type views:MvxWindowsPage.
Now when I run the application, I get an exception on setup.Initialize:
Problem seen creating View-ViewModel lookup table - you have more than one View registered for the ViewModels: 2*PaymentListViewModel (PaymentListView,PaymentListView)'
Is there a way to solve this or do I have to make a giant Visual Trigger an press both UI's in one XAML?
During creating an example I found the issue.
I have all my views in a folder views. When I created a new XAML view in that folder the class path was something like this:
x:Class="MoneyFox.Windows.PaymentListView"
Therefore it wasn't correctly mapped to the PaymentListView.xaml.cs who has the namespace MoneyFox.Windows.Views.PaymentListView.
As soon as I adjusted that, it worked.

Using single View for multiple ViewModels with Caliburn.Micro on Windows Phone 8.1

I have a Windows Phone 8.1 app using Caliburn.Micro. In the app I have a few ViewModels that fetch data in different way and with different logic but show them in the same way. So I want to make all those ViewModel use the same one View.
I found out that ViewLocator.LocateTypeForModelType is a method that gets executed for mapping ViewModels to Views. So I override it to use my custom attribute when present:
var original = ViewLocator.LocateTypeForModelType;
ViewLocator.LocateTypeForModelType = (modelType, displayLocation, context) =>
{
var useViewAttributes = modelType.GetTypeInfo().GetCustomAttributes<UseViewAttribute>(true);
if (useViewAttributes.Count() == 1)
{
var viewTypeName = string.Concat(modelType.Namespace.Replace("Model", string.Empty), ".", useViewAttributes.First().ViewName);
var type = AssemblySource.FindTypeByNames(new List<string>() { viewTypeName });
return type;
}
return original(modelType, displayLocation, context);
};
Stepping through the it seems to work fine. If I navigate to a ViewModel and that ViewModel has a UseView, my method returs the correct View.
The app navigates to the correct View but the ViewModel is never created. Kind of like Caliburn.Micro forgot about the ViewModel, or was looking for one using a different convention, or something.
I found out that ViewModelLocator.LocateTypeForViewType is called after navigation to a View to resolve the ViewModel. The ViewModel type from the previous step seems to be forgotten completely.
In ViewModelLocator.LocateTypeForViewType I only have access to the View type and I do not know, how to make it resolve the correct ViewModel from the previous step. I could scan all the ViewModel and find the ones with the correct attribute, but I would not know which one to choose.
Any ideas on how to approach this?
Here is a minimal project showing my setup: https://dl.dropboxusercontent.com/u/73642/CMVMTest.zip
This sort of solution would work everywhere else except for the top level navigation. The reason for this is there is sort of a "double dispatch: going on when you navigate.
As you know the Frame or PhoneNavigationFrame control (depending on WinRT or Silverlight) is view based in it's navigation. So the steps look a little like this.
Your code tells the navigation servie=ce to navigate to ProductViewModel.
It uses ViewLocator (where you've injected your code) to locate ProductView and tells the Frame to navigate to that.
The navigation service then responds to the navigating event to ProductView and locates the correct view model using ViewModelLocator.
It then instantiates and binds this view model.
This sort of view model to view to view model step in navigation service causes the hiccup in your code.
You should be able to create dummy views that simply inherit the base view and add nothing. So if you have MySharedView.xaml then declaring what's below should be enough.
public class SecondView : MySharedView { }
It's not ideal I know, but does get you the reuse you're after. Having the navigation service remember the view model between the navigating and navigated events becomes complicated with all the external factors that can cause navigation as well.

hide UINavigationBar from #selector

I have a simple WebView application. In one of the cases, I show a file, to allow user go back a UINavigationBar is shown:
navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 20, 320, 44)];
navigationItem = [[UINavigationItem alloc] init];
navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Back"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(buttonClicked:)];;
[navigationBar pushNavigationItem:navigationItem animated:YES];
[self.view addSubview:navigationBar];
This is working nice. It shows a back button, and when back button is clicked it executes selector (button style must be improved, but I'm newbie with xCode and c# and this will come later).
In the selector i load previous url, but I can't figure out how to hide button and bar.
- (IBAction) buttonClicked:(id)sender {
return [self.webView loadRequest:[[NSURLRequest alloc] initWithURL:[NSURL URLWithString: lastURL]]];
}
I've tried several things, like removeFromSuperView:
[navigationBar removeFromSuperview];
Hide it:
[self.navigationController setNavigationBarHidden:YES animated:YES];
self.navigationController.navigationBar.hidden = YES;
// or
[navigationItem hidesBackButton];
// or
navigationBar.hidden = TRUE;
Using the selector (id):
[sender show:NO];
Using the alpha property:
navigationBar.alpha = 0,0;
Free memory space:
navigationItem.dealloc;
But nothing works... I think I'm missing some important detail, because I don't know really how xCode works...
Any help will be appreciated!
Personally, I would never add a navigation bar like that, but rather have a root navigation controller, which has a root view controller that holds a webview as it's view.
You did not make it very clear, how do you actually show the file?
Anyway, this code:
[self.navigationController setNavigationBarHidden:YES animated:YES];
is not working for you because you haven't provided your view controller with a navigation controller.
Provide one, maybe best in the main storyboard, and setup your view controller (that holds the webview) as its root view controller. To do that, open the storyboard, and drag/drop a UINavigationController to the main area. You have to tick the option "Is Initial View Controller". Note: when you did the drag/drop, a default root view controller was already provided. You can either leave that one, or provide your own, if you already had one in the storyboard. To do that, delete the linked one, and ctrl-click and drag from the Navigation Controller to the View Controller, to setup it as its Root View Controller. I've attached an image to showcase what I've described.

Display form in pop up

i have two cshtml page one which has the link the makes the popup appear, and another with just the form data and would like to know how i would be able to display the form on the popup, as i am using MVC, to create the form, the plage which has link on is in the ~/views/client/index.cshtml while thr form is ~/views/fb/CreateOrEidt.cshtml the colller is called "FB" and the methos to call is edit, it take the parameter Id
I have tried #hmtl.renderpartial, #html.renderaction, #hmtl.partial, #html.action,
I have also tied these method with a { after the # and the end, doesnt give me a error bust still doesn't display information
The error which i get is razor canot convert type object to void
It's a little unclear what you're trying to achieve but from the error you've mentioned when calling Html.RenderAction you need to call using something like:
#{
Html.RenderAction("ACTIONNAME");
}
Calling the other methods against an action view won't work.
If you're trying to display a view inside a popup though you want to be careful that you don't end up including your layout page etc as this is potentially not the look you're going for..!
The pattern I generally implement for things like this is to simply partial out the form bit that I need to reuse and call renderpartial to display this where I need it. This renders just the html I'm after then and not the whole view (+ layout(s)).

Determine if View is being rendered as Partial

Is there a way to determine if a view is rendering as a partial?
I'm hoping to extend the reuse of a partial that I'm writing by catching this...and if necessary assigning the appropriate layout to the View.
At the moment I'm just rendering it in a div, but I could also see us using it as a modal and possible it's own page.
(the modal shouldn't require any change so no worries there)
EDIT:
To clear up what I'm asking.
I'm wondering if there is anyway to determine the difference between a view being rendered by...
/path/to/controller
and
Html.Partial("/path/to/view.cshtml")
Why not #if (Layout==null)?
Still I would recommend another view for the "own" page and set the layout there.
In your view (assuming Razor syntax):
#if(typeof(this) == Controller.PartialView)) //code
or
#if(this is Controller.PartialView) //code
Based on #Pheonixblade9 's response and the lack of other answers it doesn't appear like this is possible at the moment. I ended up just binding the Model of the View as bool and pass this value in when rendering the view/partial.

Categories

Resources