Reference .xaml file to a .cs file from another folder - c#

Can anyone help me with a problem? I found some solutions but it didnt worked for me. I have to use a part of xaml code to implement a fuctionality on a service, more exacty I have to open location settings from the service soo, in that way I need some xaml code. How exactly can I reference a .xaml file from views to a .cs from services?

From what you gave us, it looks like LocalizareFarmaciiPage.xaml is a Page. It means that it has a code-behind file (which is a .cs file).
If you want to instantiate your page in order to display it, you must add an using statement followed by the namespace your page file is stored in. I believe the namespace you're looking for is ProjectName.Views, so you want to add
using ProjectName.Views
with ProjectName being replaced by your project name of course. Then you should be able to access the LocalizareFarmaciiPage class:
LocalizareFarmaciiPage page = new LocalizareFarmaciiPage();
page.Show();
Also, if I may, interacting with views from services may not be a good idea, as it breaks the responsibility of the services. I believe that only ViewModels should have this responsibility (and you may want to take a look at the MVVM design pattern for this matter). Services should only be responsible for business-logic matters.

Related

Multiple components use the same tag in blazor

I'm currently playing around with blazor just to test what it is able to do.
I have a main project which will act as the final website to be shown to the user. For the components I have created a class-library for holding bootstrap-based components like a Table which will render the table with bootstrap-class applied.
Because of I will have multiple websites at the end, there will also be shared components between those in another class-library project. This one will also have a component called Table which will render a bootstrap-table from the other shared project with additional handlings for sorting, paging, filtering and so on.
The problem I get is, that there is a naming-conflict which I am not able to resolve.
Lets say the the projects are named Company.Website1 for the final website, Company.Shared.Components for the extended table and Company.Shared.Components.Bootstrap which will hold the bootstrap-components to be consumed by the other shared project.
When I try to create my Table-component in Company.Shared.Components I get the following error
Multiple components use the tag 'Table'
I tried whats been written here but then I got the error
Found markup element with unexpected name 'Table.Table'. If this is intended to be a component, add a #using directive for its namespace
I also tried to alias the using directive without any chance.
The razor-file itself is simply
#using Company.Shared.Components.Bootstrap.Table
<Table></Table>
I guess I would get the same errors if I would use a third-party library which has some components named the same as some already existing in my project. So there must be a workaround which I'm currently not able to see.
If you have a multiple components that share the same name, you can just add the namespace in the tag to specify which one you want to use.
So you could do:
<Company.Shared.Components.Table></Table>
Or
<Company.Shared.Components.Bootstrap.Table></Table>
Source: https://learn.microsoft.com/en-us/aspnet/core/blazor/components/?view=aspnetcore-5.0#namespaces
Had this issue with Blazorise.Icon vs Blazorise.Icons.FontAwesome.Icon - anywhere that imported both Blazorise and Blazorise.Icons.FontAwesome hit a problem
In the end, to keep things clean, I just inherited Blazorise.Icons.FontAwesome.Icon:
//in FAIcon.razor
#inherits Blazorise.Icons.FontAwesome.Icon
#{
base.BuildRenderTree(__builder);
}
and used <FAIcon everywhere
I had this issue after moving a blazor component with code-behind cs file. Moving the whole package into a different folder created a namespace conflict in the cs file because the namespace was not automatically updated while the one from the .razor file seems to reflect the folder structure.
Updating the namespace in the .cs file to match the new folder structure resolved the issue for me

WPF ResourceDictionary converter resource not found when startup project is a class library

As a precursor to this long winded explanation, I have looked thought the existing posts and have not found what i was looking for, I have also googles the issue and also came up with nothing that solved my issue.
I'm a self thought C# WPF noob.
This is my first post, I hate asking for help as I hate wasting other people's time. But I find myself in a situation that I have not been able to solve. So if anyone can help shed some light on this I would be greatly appreciated.
I am working on an Autodesk Revit plugin. The Solution consists of three projects,
Addin Project, is a class library that implements Autodesk Reivt IExternalApplication interface that registers with Autodesk Revit.
Manager Project , is a WPF application that is called from the Addin class library project when the user clicks a button in the Revit User Interface.
Installer, (Not relevant to this issue)
My issue is related to the resources and MergedDictionaries in the WPF application.
I have style resources for the various controls that I need to style and also data and converter resources.
I add the resources to the App.axml resources section and also to the pages in the project. The reason for that is part of my issue.
When the user clicks to open the MainWindow from the Addin Project, the resources give issues in that they cant find "x" resource. The message looks like this:
"Exception: Cannot find resource named 'FamTypeConverter'. Resource names are case sensitive."
The converter is being used in a style for ListView CellTemplate which is in another recourse file.
The strange thing is that the resource file contains other resources that work just fine. But this one in particular is giving me issues. The resource is a converter that implements IValueConverter and currently does no conversion while I troubleshoot this issue, it just returns the value for the convert and convert back methods.
I have another converter in the same *.CS file called "FileSizeConverter" that simply converts an INT input into a formatted file size string. The Resources is in the same dictionary and points to it's converter in the same *.CS file, different class (obviously), witch one works perfectly.
I have tried using static and dynamic recourses, setting the build action of the resource xaml files to page, resource, Embedded Resource with no change in results.
I have tried adding the resource to the pages them-selves and tried relative and Pack url's. nothing seems to work. I also have to add the MergedDictionaries to the main window, pages and the app.xaml for the resources that do currently work, to work. if I don't add the resources to the pages I get the same error, cannot find "x" resource.
If I set the start up project to the WPF project, then there is no issue. All resources work.
I think the issue is that the starting project does not load or cache the resources in the WPF project when the Window is in another project.
I'm 80% sure this is related to the fact that the WPF application extends "Application" and the class library does not! I presume the application class is where the resources are loaded but this is where my lack of knowledge kicks in.
I have no clue if I am on the right track or if I have missed something so obvious that I will want to kick myself when I realize what is wrong.
Sorry for the long winded explanation, and I hope that I have made myself clear and concise. If any clarity is required please feel free to request.
Thankyou in advance
Dereck Clements
I ended up moving any GridView styles to the page rather than a resource dictionary. I Think it was related to the converter being used in the resource dictionary that caused the issue to begin with. Having the GridView defined on the page solves the problem.

Accessing Umbraco.TypedContentAtRoot() in another assembly

I'm trying to generate an xml sitemap for published Umbraco content. Ideally I'd like to access UmbracoHelper.TypedContentAtRoot() or something similar that gives easy access to published items. I need to access it outside the web solution, inside a supporting assembly where I'm generating xml sitemaps and sitemap index for dynamic content outside the direct control of Umbraco.
Any ideas about the easiest way to go about this? I've tried referencing various Umbraco assemblies and digging into what's readily available but haven't found anything immediately useful yet.
I have stumbled across
UmbracoContext.Current.Application.Services.ContentService.GetRootContent()
but this seems to require a much deeper understanding about how Umbraco content works under the covers in order to drill down to extract published content for me to index.
Can anyone point me in the right direction? Thanks in advance!
Assuming the DLL you are creating is going to be using IN the umbraco site, you will be able to access UmbracoContent.Current in your classes, as long as you've got a reference to the Umbraco.Core package, as Mivaweb states.
If on the other hand you want to access the UmbracoContext from a completely unrelated external application (say an executable file), then that it MUCH harder to do. A few people have got it sort of working, but it's a bit flakey. Usually in this case, you're better off adding some kind of WebAPI controller action that you can call via HTTP from your executable file, so you have full access to Umbraco on the site.
Update:
If you just want to access the typed content, you can get at it by using:
UmbracoContext.Current.ContentCache
This will allow you to do stuff like:
UmbracoContext.Current.ContentCache.GetAtRoot();
Which will get you all of the root content of the cache.
If you have a seperated project where you would like to do Umbraco stuff then go to the nuget package manager and install Umbraco.Core.
This will install only the necessary Umbraco dll's that you then can use.
Then you just need to include the namespace and you are ready to go.

Auto-Generating namespace for the class added in a folder in Visual Studio

I have take App_Code folder to my solution and added two more folders saying Business and Data. Now when I add a class to these folders the class added should be under the same namespace as name of the folder.
I,e If I add a class under Business that class should come under Business namespace.
similarly, If I add a class under Data that class should come under Data namespace.
I am bit new to this. Please help me in getting this done.
I think what might have gone on here is that you were in the Folder View of the Solution Explorer when you made your classes.
If you click the icon to the right of the home icon in the Solution Explorer Toolbar (see the image below), then you should be able to switch back to the Solution View. Now when you add a class the correct namespace will be generated automatically in the template. A lot of other convenient features are not available when making new classes in the Folder View, too, not exactly sure why!
"Switch between solutions and available views"
I know this is an old question, but I had the same problem and couldn't find any solutions in all my googling, so I felt I had to answer.

Why doesn't this inherited view render?

I tried the following:
I have a shared library (.dll) that contains these files:
Views
Search
PowerSearch.aspx
PowerSearch.aspx.cs
PowerSearch.aspx.designer.cs
The PowerSearch.aspx file contains my html code.
The PowerSearch.aspx.cs file contains this:
using System.Web.Mvc;
using CommonProject.Web.Shared.Controllers;
namespace CommonProject.Web.Shared.Views.Search
{
public partial class PowerSearch : ViewPage<SearchViewData>{}
}
And the designer I catually don't even care about cause it's not used anyways.
Nothing fancy, just a strongly typed view.
I basically pulled an existing, working view out of my asp.net mvc project, put it in a seperate library and changed the namespace to contain the word "Shared" as in "this will be shared amongst several mvc projects".
Then in my original asp.net mvc project I created the same structure, only now the aspx page contains nothing but the asp #Page rule.
The matching cs file contains:
using System.Web.Mvc;
using CommonProject.Web.Shared.Controllers;
namespace CommonProject.Web.DRE.Views.Search
{
public partial class PowerSearch : CommonProject.Web.Shared.Views.Search.PowerSearch { }
}
There are no compile errors and no run time exceptions either. There is only a huge blank page...
Anybody got an idea?
Make sure the assembly (dll) is in your bin folder that you are referencing. Also try adding the assembly namespace to your web.config otherwise your View will not be able to find the inherited page.
<pages>
<namespaces>
<add namespace="CommonProject.Web.Shared"/>
How much of a struggle was it to try to add an ASPX page to a class library?! It's not in the default list when you select 'Add New Item...'.
I know it's OK to store Controllers and Models in a separate library DLL/assembly, but I'm pretty sure it's not as trivial to store or share the Views. The problem is that the default view engine is looking in a specific folder on disk for the view (~/Views/Controller/ViewName.as[pc]x or ~/Views/Shared/ViewName.as[pc]x). With a library DLL, the compiler doesn't really have any idea of what it can do with your ASPX file. It's not code, so unless you have a 'Build Action' set, it's just going to ignore it. There are various 'Built Actions' but I think your only options are 'Copy' and 'Embed As Resource'. Copy isn't going to copy to the folder that you need it to (the Views folder in your ASP.NET MVC web project), although you could possibly write a build script or 'Custom Tool' that did that for you (with a bit of work).
Hammett (of Castle Monorail) fame (and now an MS Employee) came out with a sample that allows you to store Views inside library assemblies using a custom VirtualPathProvider class that is able to dig into the DLL and pull out the View (embedded as a resource). The sample application is just a concept right now, so you might hit some roadblocks, but it appears to work and looks like an exciting direction. You can find it on his blog here: MEF and ASP.NET MVC sample. Download the code and do some exploring.
This blog post ASP.NET MVC Plugins is not by the same author as the one above, but it gives another examination of the topic and points to another post here on StackOverflow where a similar question was asked: Using VirtualPathProvider to load ASP.NET MVC views from DLLs.
I've seen a post from Phil Haaaaaaaaaaaaaaaaaack on storing views in the database. It's using Ruby scripts instead of Web Forms, so I'm not 100% sure if you could adapt his sample to fit your needs or not. Check it out here: Scripting ASP.NET MVC Views Stored In The Database.
What does the source look like for the .aspx file? Make sure you are inheriting your view in the page directive like this:
<%# Page Title="" Inherits="CommonProject.Web.Shared.Views.Search.PowerSearch" %&>
Also, if you don't want to specify the whole namespace in your page directive, you can add this to the <namespaces /> section in your web.config:
<add namespace="CommonProject.Web.Shared.Views.Search"/>

Categories

Resources