I'm pretty new to C#, I'm trying to complete a little side project I've been working on that uses a small amount of C# code to assist the development of a Windows Desktop Gadget. Basically, I'm trying to implement the IDesktopGadget interface so that I can use the RunGadget method.
Here's what I got so far from reading information about similar interfaces:
[ComImport]
[Guid("C1646BC4-F298-4F91-A204-EB2DD1709D1A")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IDesktopGadget
{
uint RunGadget([MarshalAs(UnmanagedType.LPWStr)] string gadgetPath);
}
Unfortunately, I get an error when I try and create an object from it: "Cannot create an instance of the abstract class or interface 'GadgetTask.IDesktopGadget'"
Can someone point me in the right direction and maybe help me understand what I'm doing wrong at the same time?
You actually need an implementation of the DesktopGadget object in order to use the interface. MS provide a standard COM object to do it on Windows 7. You can create an instance by doing something like:
Type t = Type.GetTypeFromCLSID(new Guid("924ccc1b-6562-4c85-8657-d177925222b6"));
IDesktopGadget dg = (IDesktopGadget)Activator.CreateInstance(t);
Thanks for the guidance. For a little more straight forward help, this is what worked for me:
IDesktopGadget.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace GadgetActivator
{
[ComImport]
[Guid("C1646BC4-F298-4F91-A204-EB2DD1709D1A")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IDesktopGadget
{
uint RunGadget([MarshalAs(UnmanagedType.LPWStr)] string gadgetPath);
}
}
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace GadgetActivator
{
class Program
{
static void Main(string[] args)
{
Type t = Type.GetTypeFromCLSID(new Guid("924ccc1b-6562-4c85-8657-d177925222b6"));
IDesktopGadget dg = (IDesktopGadget)Activator.CreateInstance(t);
dg.RunGadget(#"C:\Program Files\Windows Sidebar\Gadgets\xxxxxxxxx.Gadget");
}
}
}
Related
I've recently tried to customize the tabbar using a custom ShellRenderer in my app using Xamarin and Visual Studio 2019. I've found this question where someone does exactly what I need and the question has an answer which explains it in great detail.
Now, my problem is that when trying to build the app, I get the following error:
'CustomBottomNavAppearance' does not implement interface member 'IShellBottomNavViewAppearanceTracker.SetAppearance(BottomNavigationView,IShellAppearanceElement)' (Errorcode CS0535, line 32)
Now, the sample project that the user in the other question provides works perfectly on my machine, yet implementing it in my project doesn't work, although they are practiacally identical.
I expect this to be very obvious for an advanced user, so sorry about that, I'm still learning and would like to just implement this feature right now, even if my skills might not allow me to do that.
Thanks in advance for any replies!
This is my 'MyShellRenderer.cs' class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Google.Android.Material.BottomNavigation;
using MyProject;
using MyProject.Droid;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
[assembly: ExportRenderer(typeof(AppShell), typeof(MyShellRenderer))]
namespace MyProject.Droid
{
public class MyShellRenderer : ShellRenderer
{
public MyShellRenderer(Context context) : base(context)
{
}
protected override IShellBottomNavViewAppearanceTracker CreateBottomNavViewAppearanceTracker(ShellItem shellItem)
{
return new CustomBottomNavAppearance();
}
}
public class CustomBottomNavAppearance : IShellBottomNavViewAppearanceTracker
{
public void Dispose()
{
}
public void ResetAppearance(BottomNavigationView bottomView)
{
}
public void SetAppearance(BottomNavigationView bottomView, ShellAppearance appearance)
{
IMenu myMenu = bottomView.Menu;
IMenuItem myItemOne = myMenu.GetItem(0);
if (myItemOne.IsChecked)
{
myItemOne.SetIcon(Resource.Drawable.icon_about);
}
else
{
myItemOne.SetIcon(Resource.Drawable.icon_feed);
}
}
}
}
Edit: I've just found out that the issue actually only exists with the Android version (well, the iOS version at least doesn't throw the error).
Update: I have been able to fix the issue by using "using Google.Android.Material.BottomNavigation;" "using Android.Support.Design.Widget;". My guess it that the old one is now deprecated and only works with the 4.x.x releases, not my 5.x.x release. Thanks everyone!
Per this docs,The error does not implement interface member Errorcode CS0535 means A class must implement all members of interfaces from which it derives or else be declared abstract.So the issue must be that the method SetAppearance has not been correctly implemented.When I Remove or Comment the SetAppearance method, the error appears.
The solution is that using using Google.Android.Material.BottomNavigation instead of using Android.Support.Design.Widget.
Very basic knowledge on c# and is the first i use anything p/invoke related.
Help me pls, i have made this code but it doesnt seem to work.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
[DllImport("C:\\Users\\lchris\\Desktop\\SevenZipLib_9.13.2\\SevenZipLib\\SevenZipLib\\7z86.dll")]
public static extern void SevenZipArchive(string c);
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
using (SevenZipArchive archive = new SevenZipArchive("file.rar"))
{
foreach (ArchiveEntry entry in archive)
{
Console.WriteLine(entry.FileName);
}
}
}
}
}
It tells me that SevenZipArchive is a 'Method' and is being used like a 'type'.
I have include the library to my project already, i just dont know how to use it.
Here is the library:
https://sevenziplib.codeplex.com/
You first need to remove this code:
[DllImport("...")]
public static extern void SevenZipArchive(string c);
You do not need to provide any p/invoke declarations. The library wraps that up for you.
This is a .net assembly. You use it just as you use any other. Take the following steps:
Download the project. You already did that I think.
Build the solution. Make sure that
In your project, add a reference to the assembly that you built in the previous stage. It's SevenZipLib\bin\Debug\SevenZipLib.dll or SevenZipLib\bin\Release\SevenZipLib.dll depending on the target you selected.
Add using SevenZipLib; to your project code to to gain access to the namespace.
Once you've done that your code will work. You can use the tests project that is supplied as part of the download as a rich source of example code.
I'm currently in the process of writing analysis code for my job. I have to "map" the code from FxCop to our own private framework. This is a bit of an issue, but not my main concern. I can't access the FileInfo class, my ultimate preview 2015 VS asked me if I wanted to generated it. So, basically, I'm asking what's the equivalent of the FileInfo class while doing code analysis? I've looked into the FileVersionInfo class, but I'm not quite sure if that's the way to go ?
EDITS
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace SomeNamespace
{
public class SomeFileValidationClass
{
public FileInfo FileInfo { get; private set; }
public SomeFileValidationClass(FileInfo fileInfo)
{
this.FileInfo = fileInfo;
_failedResults = new List<SomeRuleResult>();
}
//Code missing, but the point still remains that FileInfo can't be accessed.
}
According to the documentation FileInfo is located in the System.IO namespace of mscorlib, so it should be present without needing any other references.
My only suggestion is that you are compiling against some odd version of mscorlib that doesn't have it for some reason, maybe its a bug in VS2015, or perhaps you have downloaded and built corefx?
Hi will write a dll in c# with not without namespace.
I'll write a class then converting it to dll, i.e c# default library type is
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace temp
{
public class c1
{
public static string f1(string deger) {
return deger.ToUpper() + " 333 ";
}
}
}
now I will write this code so :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
public class c1
{
public static string f1(string deger) {
return deger.ToUpper() + " 333 ";
}
}
is that twou codes are same on runtime
Then this class will be at .NET unnamed global namespace(not in assembly root namespace). And it can be accessed from anywhere as "global::c1" or just "c1"
By convert this code to dll I think you mean to compile as a library. The namespace (or lack of) has no special effect in this regard.
If you want to reference this class without referencing the dll you will need to load the dll dynamically.
Example:
Assembly a = Assembly.LoadFrom("mylibrary.dll");
Your next problem is to reflect and use the class. Usually you would have a library that defines some interface or interfaces.
InterfaceLibrary.dll Contains interface IC1
PluginLibrary.dll Contains class C1 which implements IC1
Program.exe
Both the Program and the plugin library refer to the interface library. The program can dynamically load PluginLibrary and look for classes that implement IC1.
Here is a more in-depth example of a plugin architecture in C#: https://stackoverflow.com/a/829828/360211
I am trying to get the SuperSocketServer app up an running. However, I cannot seem to create an instance of the server. Here is my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SuperSocket.SocketBase;
using SuperSocket.SocketBase.Config;
using SuperSocket.SocketEngine;
using SuperSocket.SocketBase.Protocol;
namespace WinnieServer
{
class Program
{
AppServer W = new AppServer();
static void Main(string[] args)
{
Console.Write("Starting...");
while (true)
{
}
}
}
}
I tried the examples in the source code. However, they all give the same error:
Cannot create an instance of the abstract class or interface 'SuperSocket.SocketBase.AppServer'
Is this the correct way to create an instance of SSS?
take care,
lee
I had this same issue. The download on their home screen gives you the 1.4.4 version. If you look in object explorer it IS abstract. If you go to their downloads and grab the binaries zip, you'll get the 1.5 version that ISN'T abstract.