Why can't I use a Partial Class in a VSTO Ribbon? - c#

I'm trying to break up a very big VSTO IRibbonExtensibility class into multiple smaller classes to make things more manageable.
I tried creating a partial class with all my Visibility callbacks in it.
The code compiles fine but doesn't work in MS Word.
Error: The call to GetCustomUI() for RibbonID 'Microsoft.Word.Document' failed.
Any idea why this isn't working??
[ComVisible(true)]
public partial class Ribbon : O.IRibbonExtensibility
{
..
}
public partial class Ribbon
{
..
}

Sorry, my bad..
I had moved the Ribbon.xml file into a new folder and that caused the issue

Related

uwp inheriting from base page giving compile errors

I am trying to inherit from a base page instead of Normal Page control of XAML but Visual Studio is giving me compile time errors that OnNavigatedTo method not found to be overridden.
I am doing this because I have multiple pages with a lot of similar code and I want to write that code just once, so I want to write that code in parent class and then inherit all pages from that parent class, I followed guidelines as stated on multiple resources on internet and below is my code.
Parent Class:
public class VideoParentPage : Page
{
}
Child Class (AllVideoPage.xaml.cs):
public sealed partial class AllVideosPage : VideoParentPage
{
public AllVideosPage() : base()
{
InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
SetBanner();
}
}
XAML in file AllVideosPage.xaml:
<local:VideoParentPage
xmlns:local="using:Fluent_Video_Player.Views.Shared"
...some unrelated XAML code...
</local:VideoParentPage>
Note: I have used all necessary using statement in C# classes.
You should try cleaning the solution Build > Clean Solution, closing VS, removing the remaining bin and obj folders and then compiling the app again.

Multiple Ribbons in Outlook Addin

I have an Outlook addin where I need to display a ribbon in the main Outlook window and in the Mail Read window as well. To do this I have added two ribbon xml files with the right markups in them. I then added a C# class that implements the Office.IRibbonExtensibility interface where I have implemented the GetCustomUI method which returns the right XML. Finally I did this in the ThisAddIn.cs class
protected override Office.IRibbonExtensibility CreateRibbonExtensibilityObject()
{
try
{
_ribbon = new Ribbon();
return _ribbon;
}
catch (Exception e)
{
}
return null;
}
So far so good. The ribbons load and everything shows in correct place.
Now the problem is that this Ribbon.cs file is getting rather huge as all the callbacks live in this file. Is there a way to split the callbacks into multiple classes? So if I have a Ribbon1.xml and RIbbon2.xml can I have equivalent Ribbon1.cs and Ribbon2.cs?
OK so as it turns out this is really not possible in the VSTO model. You can really only have one class which must have all the event handlers in it. The recommended approach is to use partial classes and split the code between multiple code files.

Deleted class still works - VS 2012

I wanted to implement a simple activation function to my C# application. It was something like this:
class Activation
{
public Activation()
{
Some code...
}
public void SomeFunction()
{
Some code...
string s = NumberOfDaysToExpire();
}
}
The complete code is not a problem. The class sets some values to the Windows registry. I decided to launch the instance of this class in the main form's constructor and it worked fine.
The problem appeared when I decided to exclude this class from the project. The code shouldn't work but it still works !!! There is no declaration of this class in the whole project - so why does it still work ? I think this is a Visual Studio 2012 problem. Please help me.
Regards,
Mariusz

Losing UserControl when editing winForm VS2012

I have a Form called EmployeeForm inside this form i'm including some UserControl and when I edit EmployeeForm every UserControl inside the form are lost.
This picture show a diff between TFS(left) and Local(right) file, after modifying the name of a combobox
Example of this.ucEmployeeKeyOne :
public partial class Employee_EmployeeKeyOneRelationUC
: Employee_EmployeeKeyOneRelation_GenericUC
{ [other Code Here] }
public class Employee_EmployeeKeyOneRelation_GenericUC
: RelationUC<MyObject>
{ }
and the definition of RelationUC :
public partial class RelationUC<T>
: DataUserControlBase
{ [other Code Here] }
public partial class DataUserControlBase
: UserControlBase
{ [other Code Here] }
public partial class UserControlBase
: System.Windows.Forms.UserControl, MyInterfaceHere
{ [other Code Here] }
All UserControl i'm losing are inherited from RelationUC<T>. Does the generic type of RelationUC may cause the problem?
For the Vs designer to load the controls, the control should be able to initialize including all public properties and you must have a empty constructor. If you don't have an empty constructor or have properties that are returning from the inherited nullable class, it is most likely the the designer will crash.
To Debug this,
1) open you project in VS but do not open the file that contains the controls yet;
2) open another VS and attach the process of VS that your project is opened;
3) set the break on exceptions (short cut 'CTRL + D, CTRL +E' ) to all (once you become familiar with what exception the designer is throwing, you can set that exception only.);
4) Go to the VS with you project and open the file that contain your user control. when the exception is thrown, the second VS will catch it and tell you exactly why and where.

Using a class in WPF

When I'm developing a ConsoleApp there is no problem to use classes in my Main that I've created in separated files (Project Menu --> Add Class). But later, when I try to do it in WPF that class is not recognized. I have made sure that the namespace it's the same both in my "MainWindow.xaml.cs" and my Class Canal.cs. When I define that same class but inside MainWindow.xaml.cs everything works fine, but due to the extension of the code I prefer separate it.
MainWindow.xaml.cs:
//using
namespace Tcomp
{
public partial class MainWindow : Window
{
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{ //Stuff but I can't use class created outside of MainWindow.xaml.cs
}
}
}
Canal.cs
//using
namespace TComp
{
public class Canal
{ //some propreties here
}
}
Thanks.
Create the class inside a library project not on a console app. And on your WPF project, add a project reference to the library project you have created.
#mcxiand already answered your question. I would like to add another option: you can use a
public partial class MainWindow : Window
and add it to as many files as you want containing your code, thus there will be no need to create additional class library. The key word here is partial, which allows the code encapsulated in this class to spread over multiple files (.cs).
You must either instantiate the Canal class:
var myClass = new Canal();
and then you can use the properties from it. Make myClass a private member of your MainWindow.xaml.cs and you can access it anytime. Or the second way, make Canal class static and then you can access it from everywhere. Hope this helps.

Categories